g | x | w | all
Bytes Lang Time Link
053Uiua251009T145009Zojdo
108C gcc251004T004341Zc--
073R251003T202252ZM--
111Swift 6250425T151300ZmacOSist
048Raku Perl 6 rakudo250425T142835Zxrs
072CASIO BASIC CASIO fx9750GIII250425T142114Zmadeforl
091AWK250425T135233Zxrs
016Pip250326T225425ZDLosc
010Japt v2.0a0 x201109T121114ZShaggy
881PHP170729T203855ZTitus
107Mathematica170809T215333ZZaMoC
034Perl 5170730T025315ZXcali
062JavaScript170729T134355Zuser7234
137C#170731T122234ZTheLetha
133C# .NET Core170731T073850ZIan H.
068Python 3170731T132903ZC McAvoy
082Octave170729T185935Zბიმო
134Swift 4170730T061106ZMr. Xcod
074Python 2170729T155926Z0xffcour
084Python 3170729T135744ZMr. Xcod
015Husk170729T190507ZZgarb
023Brachylog170729T175633ZErik the
056Haskell170729T145007Znimi
020Pyth170729T144752ZMr. Xcod
018Pyth170729T144730ZErik the
016Japt v2170729T144517ZETHprodu
00705AB1E170729T140901ZErik the
012Jelly170729T140640ZJonathan
017Jelly170729T132637ZErik the

Uiua, 53 bytes

/+♭⬚0⋕⊂□⊙□⍉.⍥(.)-⊃(/+∊".-")⧻.

My first Uiua program, probably quite suboptimal. I hope I counted the bytes right (wc -c my-answer.uiua). Still lacking: correct and flexible rounding for the floating point numbers, though the result is "correct" numerically.

Uiua pad this online

Breakdown

Takes input as a string on the stack.

                                . # Duplicate input, then
                     ⊃            # both...
                               ⧻  # ... count characters and..     
                      (/+∊".-")   # ... count [.-] occurrences
                    -             # subtract both counts
               ⍥(.)              # repeat input that often into an array
            ⍉.                   # transpose a copy of that array
        □⊙□                      # "box" both arrays
       ⊂                          # and join them
    ⬚0⋕                           # convert strings to numbers, default 0
   ♭                              # flatten the result to 1D
 /+                               # sum-reduce

C (gcc), 108 bytes

c,e;f(char*s,double*r){double atof(),d=atof(s),f=*r=0;for(e=1;c=*s++;)c>47?e*=10,f+=c-48,*r+=d:0;*r+=e/9*f;}

Try it online!

R, 79 73 bytes

-6 bytes by Giuseppe

\(n,s=el(strsplit(c(n,""),"")),`~`=sum)strtoi(strrep(s[z<-s>"/"],~z))~z*n

Attempt This Online!

Swift 6, 170 111 bytes

{s in{d in.init(d.count)*Float(s)!+d.reduce(0){$0+Float({String.init}()($1,d.count))!}}((s+"").filter{$0>"/"})}

Try it on SwiftFiddle!

Raku (Perl 6) (rakudo), 48 bytes

{$_*($!=(my@z=.comb(/\d/)))+[+] @z.map:{$_ x$!}}

Attempt This Online!

{$_*           # input number multiplied by
($! =          # number of digits
(my@z=         # create array
.comb(/\d/)))  # of only digits
+              # add to it
[+]            # coerce int from string
@z.map:        # each element of array
{$_ x$!}       # string of chars times number of digits
}              # implicit return

CASIO BASIC (CASIO fx-9750GIII), 72 bytes

?→Str 1
StrLen(Str 1)→E
Exp(Str 1)E→A
For 1→I To E
For 0→J To E-1
A+₁₀J(Exp(StrMid(Str 1,I,1))→A
Next
Next

yerp

AWK, 91 bytes

{for(x=$0*(y=length-gsub(/-|\./,X));i++<split($0,a,X);x+=g)for(g=j=0;j++<y;)g=g a[i]}1,$0=x

Attempt This Online!

{               # implicit input
for(            # main loop
x=              # running total
$0*(            # input string multiplied by
y=              # length of just digits
length-         # length of input -
gsub(           # returns subs count
/-|\./,         # sub - and .
X));            # deletes - and .
i++<            # for each char
split($0,a,X);  # split new number into digits
x+=g)           # add new column
for(            # append loop
g=j=0;          # reset          
j++<y;)         # append times length of digits
g=g a[i]        # append char (1 1 -> 11)
}1,             # print even if zero
$0=x            # set default output

Pip, 16 bytes

YaMa@XD$+:yALZJy

Attempt This Online!

Explanation

YaMa@XD$+:yALZJy
   a              ; Input number
    @XD           ; Regex match all digit characters
 aM               ; In that list, replace each value with the input number
Y                 ; Store the resulting list in y
             ZJy  ; Transpose y, joining rows back into strings/numbers
          yAL     ; Append that list to y
       $+:        ; Sum

Strings like --- or .... that are not numbers are treated as 0 in numeric contexts.

Japt v2.0a0 -x, 10 bytes

Takes input as a string.

f\d çU
cUy

Try it or run all test cases

f\d çU\ncUy     :Implicit input of string U                    > "-0.45"
f               :Match (returns an array)
 \d             :  RegEx /\d/g                                 > ["0","4","5"]
    çU          :Fill with U                                   > ["-0.45","-0.45","-0.45"]
      \n        :Reassign to U
        c       :Concatenate
         Uy     :  U transposed                                > ["-0.45","-0.45","-0.45","---","000","...","444","555"]
                :Implicit output of sum of resulting array     > 997.65

PHP, 78 88 +1 bytes

for($e=preg_match_all("#\d#",$n=$argn);~$c=$n[$i++];)$s+=str_repeat($c,$e);echo$s+$n*$e;

Run as pipe with -nR.

May yield warnings in PHP 7.1. Repace $c,$e with $c>0?$c:0,$e to fix.

Mathematica, 107 bytes

(t=Length[s=#&@@RealDigits[#]//.{a___, 0}:>{a}];If[IntegerPart@#==0,t++];t#+Tr[FromDigits@Table[#,t]&/@s])&

Perl 5, 37 33 + 1 (-p) = 38 34 bytes

$_*=@n=/\d/g;for$\(@n){$_+=$\x@n}

Try it online!

Used some tricks from Dom's code to shave 4 bytes

Explained:

@n=/\d/g;    # extract digits from input
$_*=@n;      # multiply input by number of digits
for$\(@n){   # for each digit:
  $_+=       # add to the input
  $\x@n}     # this digit, repeated as many times as there were digits
             # taking advantage of Perl's ability to switch between strings
             # and numbers at any point

JavaScript, 75 62 bytes

a=>(b=a.match(/\d/g)).map(b=>a+=+b.repeat(c),a*=c=b.length)&&a

Try it online

-2 bytes thanks to Arnauld
-5 bytes thanks to Shaggy (I though the function must receive a number, but now I see that lot of other answers receive string too)

C#, 139 137 bytes

using System.Linq;n=>{var d=(n+"").Where(char.IsDigit);return d.Sum(i=>int.Parse(new string(i,d.Count())))+new int[d.Count()].Sum(_=>n);}

Saved 2 bytes thanks to @Ian H.

Try it online!

Full/Formatted version:

namespace System.Linq
{
    class P
    {
        static void Main()
        {
            Func<double, double> f = n =>
            {
                var d = (n + "").Where(char.IsDigit);
                return d.Sum(i => int.Parse(new string(i, d.Count()))) + new int[d.Count()].Sum(_ => n);
            };

            Console.WriteLine(f(123));
            Console.WriteLine(f(-144));
            Console.WriteLine(f(4));
            Console.WriteLine(f(244.2));

            Console.ReadLine();
        }
    }
}

C# (.NET Core), 150 141 133 bytes

Saved 9 bytes thanks to @TheLethalCoder
Saved another 8 bytes thanks to @TheLethalCoder

a=>{var c=(a+"").Replace(".","").Replace("-","");int i=0,l=c.Length;var r=a*l;for(;i<l;)r+=int.Parse(new string(c[i++],l));return r;}

Try it online!

Takes a string as an input and outputs the 'squared' number as a float.


This code follows the following algorithm:

  1. Create a new string from the input, but without the decimal points and symbols, so we can get our length and the numbers for the columns from there.

  2. Calculate the input times the length of the string we created at point 1.

  3. For each column in our 'square', create a new string with the column number and the row length and add it to our result.

Example:

Input: -135.5

  1. If we replace decimal points and symbols we get the string 1355, which has a length of 4.
  2. The input times 4: -135.5 * 4 = -542.
  3. Now we create new strings for each column, parse them and add them to our result:
    1111, 3333, 5555, 5555.

If we sum these numbers up we get 15012, which is exactly what our program will output.

Python 3, 68 70 73 77 bytes

lambda n:sum(float(n)+int(_*sum(x>"/"for x in n))for _ in n if"/"<_)

Try it online!

Loops over every digit character and repeats it by the number of digit characters overall, makes that into an integer, and adds that to n. This way n gets added d times, the horizontal part of the sum, along with the digit repetition, which is the vertical part. Originally used str.isdigit but >"/", thanks to others in this thread, saved a lot of bytes. Saves two bytes by taking n as a string, but the output is messier.

lambda n:sum(n+int(_*sum(x>"/"for x in str(n)))for _ in str(n)if"/"<_)

Try it online!

Octave, 100 82 bytes

Thanks a lot @TomCarpenter for teaching me that assignments have a return value and saving me 18 bytes!

@(v)(n=nnz(s=strrep(num2str(abs(v)),'.','')-'0'))*v+sum(sum(s'*logspace(0,n-1,n)))

Try it online!

Ungolfed/Explanation

function f=g(v)
  s=strrep(num2str(abs(v)),'.','')-'0';  % number to vector of digits (ignore . and -)
  n=nnz(s);                              % length of that vector
  f=n*v+sum(sum(s'*logspace(0,n-1,n)))   % add the number n times and sum the columns of the square
end

The way this works is that we basically need to add the number itself n times and then add the sum of the columns. Summing s' * logspace(0,n-1,n) achieves the sum of columns, for example if v=-123.4 that matrix will be:

[ 1     10    100   1000;
  2     20    200   2000;
  3     30    300   3000;
  4     40    400   4000 ]

So we just need to sum it up and we're done.

Swift 4, 139 134 bytes

func f(s:String){let k=s.filter{"/"<$0};print(Float(s)!*Float(k.count)+k.map{Float(String(repeating:$0,count:k.count))!}.reduce(0,+))}

Test Suite.


Explanation


Let's take an example!

Say our String is "0.45". First off, we filter the digits, so we are left with 0, 4, 5. We convert "0.45" to Float and multiply by the number of digits: 0.45 * 3 = 1.35. Then we take each digit and turn it into a String repeating that digit until it fills the width of the square (how many digits there are): 0, 4, 5 -> 000, 444, 555. We sum this, 000 + 444 + 555 = 999. Then we just add the results together: 1.35 + 999 = 1000.35.

Python 2, 81 74 bytes

-7 bytes thanks to @Mr. Xcoder: '/'<i

lambda x:sum(float(i*len(z))for z in[[i for i in`x`if"/"<i]]for i in[x]+z)

Try it online!

Explanation:

Say 123.45 is given as input. [i for i in`x`if"/"<x] gives a list of stringified integers ['1','2','3','4','5'] (which is also z). Now we iterate through [x]+z i.e. [123.45,'1','2','3','4','5'], multiplying each element by len(z), here 5 and converting each to a Float (so that strings also convert accordingly), yielding [617.25,11111.0,22222.0,33333.0,44444.0,55555.0]. Finally we calculate the sum(...) and obtain 167282.25.

Python 3, 95 94 87 85 84 bytes

def f(i):l=[x for x in i if"/"<x];k=len(l);print(k*float(i)+sum(int(x*k)for x in l))

Test Suite.

Python 3, 78 bytes

lambda x:sum(float(i*len(z))for z in[[i for i in str(x)if"/"<i]]for i in[x]+z)

Test Suite.

The second approach is a port to Python 3 inspired by @officialaimm's solution.

Husk, 15 bytes

§+ȯṁrfΛ±TṁrSR#±

Takes a string and returns a number. Try it online!

Explanation

It's a bit annoying that the built-in parsing function r gives parse errors on invalid inputs instead of returning a default value, which means that I have to explicitly filter out the columns that consist of non-digits. If it returned 0 on malformed inputs, I could drop fΛ± and save 3 bytes.

§+ȯṁrfΛ±TṁrSR#±  Implicit input, e.g. "-23"
             #±  Count of digits: 2
           SR    Repeat that many times: ["-23","-23"]
         ṁr      Read each row (parse as number) and take sum of results: -46
  ȯṁrfΛ±T        This part is also applied to the result of SR.
        T        Transpose: ["--","22","33"]
     fΛ±         Keep the rows that contain only digits: ["22","33"]
   ṁr            Parse each row as number and take sum: 55
§+               Add the two sums: 9

Brachylog, 23 bytes

{∋ịṫ}ᶠ⟨≡zl⟩j₎ᵐ;[?]zcịᵐ+

Try it online!

Brachylog doesn't go well with floats...

Explanation:

{∋ịṫ}ᶠ⟨≡zl⟩j₎ᵐ;[?]zcịᵐ+ Takes string (quoted) input, with '-' for the negative sign
     ᶠ                  Return all outputs (digit filter)
{   }                    Predicate (is digit?)
 ∋                        An element of ? (input)
  ị                       Convert to number (fails if '-' or '.')
   ṫ                      Convert back to string (needed later on)
      ⟨   ⟩             Fork
       ≡                 Identity
         l               Length
                        with
        z                Zip
             ᵐ          Map
            ₎            Subscript (optional argument)
           j              Juxtapose (repeat) (this is where we need strings)
              ;         Pair with literal
               [ ]       List
                ?         ?
                  z     Zip
                   c    Concatenate (concatenate elements)
                     ᵐ  Map
                    ị    Convert to number
                      + Add (sum elements)

Haskell, 59 56 bytes

f s|l<-filter(>'.')s=0.0+sum(read<$>(s<$l)++[c<$l|c<-l])

The input is taken as a string.

Try it online!

How it works

l<-filter(>'.')s      -- let l be the string of all the numbers of the input string
f s   = 0.0 + sum     -- the result is the sum of (add 0.0 to fix the type to float)
   read<$>            -- turn every string of the following list into a number
   s<$l               -- length of l times the input string followed by
   [c<$l|c<-l]        -- length of l times c for each c in l   

Pyth, 21 20 bytes

K@jkUTQ+smv*lKdK*lKv

Test suite.

Uses a completely different approach from @EriktheOutgolfer's answer, which helped me golf 1 byte in chat, from 22 to 21.


Explanation

K@jkUTQ+s.ev*lKbK*lKv

K@jkUTQ                   - Filters the digits and assigns them to a variable K.
         m                - Map. Iterated through the digits with a variable d
           v              - Evaluate (convert to float).
            *lKd          - Multiplies each String digit by the length of K.
        s                 - Sum
       +                  - Sum
                 *lKvQ    - Multipies the number  by the length of the digits String

Pyth, 18 bytes

s+RvQsM*RF_lB@jkUT

Try it here.

Japt v2, 16 bytes

o\d
l
¬xpV +V*Ng

Test it online!

Explanation

o\d    First line: Set U to the result.
o      Keep only the chars in the input that are
 \d      digits. (literally /\d/g)

l    Second line: Set V to the result.
l    U.length

¬xpV +V*Ng    Last line: implicitly output the result.
¬             Split U into chars.
 x            Sum after
  pV            repeating each V times.
     +V*Ng    Add V * first input (the sum of the horizontals) to the result.

05AB1E, 7 bytes

þSDg×+O

Try it online!

Explanation

þSDg×+O Implicit input
þ       Keep digits
 S      Get chars
  D     Duplicate
   g    Length
    ×   Repeat string(s)
     +  Add (implicit input added to all elements)
      O Sum

Jelly,  13  12 bytes

fØDẋ€L$ŒV+VS

A monadic link accepting a list of characters (a well-formed decimal number, the single leading zero being a requirement for -1 < n < 1) and returning a number.

Try it online!

14 bytes to accept and return numbers (input limited at +/-10-5 by ŒṘ): ŒṘfØDẋ€L$ŒV+⁸S.

How?

fØDẋ€L$ŒV+VS - Link: list of characters         e.g. "-0.45"
 ØD          - yield digit characters                "0123456789"
f            - filter keep                           "045"
      $      - last two links as a monad:
     L       -   length (number of digit characters) 3
   ẋ€        -   repeat list for €ach digit          ["000","444","555"]
       ŒV    - evaluate as Python code (vectorises)  [0,444,555]
          V  - evaluate (the input) as Jelly code    -0.45
         +   - addition (vectorises)                 [-0.45,443.55,554.55]
           S - sum                                   997.65

Jelly, 17 bytes

ŒṘfØDẋ€L©$ŒV;ẋ®$S

Try it online!