26
\$\begingroup\$

Challenge

Given an integer, Q in the range -(2^100) ≤ Q ≤ 2^100, output the number of digits in that number (in base 10).

Rules

Yes, you may take the number as a string and find its length.

All mathematical functions are allowed.

You may take input in any base, but the output must be the length of the number in base 10.

Do not count the minus sign for negative numbers. The number will never have a decimal point.

Zero can either have one or zero digits.

Assume the input will always be a valid integer.

Examples

Input > Output

-45 > 2
12548026 > 8
33107638153846291829 > 20
-20000 > 5
0 > 1 or 0

Winning

Shortest code in bytes wins.

\$\endgroup\$

56 Answers 56

1
\$\begingroup\$

Excel VBA, 15 Bytes

Anonymous VBE immediate window function that takes input from [A1] and outputs to the VBE immediate window

?[Len(Abs(A1))]
\$\endgroup\$
0
\$\begingroup\$

Fourier, 30 bytes

I~S<0{1}{-2*S~S}S{0}{S^~S}SL^o

Try it on FourIDE!

Fourier doesn't have strings, so it uses logs instead. Bytes are wasted on supporting zero and negative numbers.

\$\endgroup\$
0
\$\begingroup\$

Befunge, 35 bytes

77*~`!1+:v
~0`#@    ># !#. #- #2_1+

Try it online!

77*~`!            add 49 to the stack and see if that is less than 50 (ascii for
                  '-') 
1+:v              if there is a -, don't count it. Add 1 so we don't
                  have a zero at the top of the stack. Move down.
># !#. #- #2_1+   move right and add one to the total. (Will remove later)
~0`#@             Loop back around and take an input and make sure it is not end of 
                  the string (-1, so we make sure it is > 0)
># !#. #- #2_1+   check to see if there was an input. If there was, add one to the 
                  total. If not, move left. Subtract 2 because we added 1 at two 
                  different locations. Print output and end program.
\$\endgroup\$
0
\$\begingroup\$

Bean, 20 bytes

Hexdump:

00000000: 2650 80d3 d080 a05d 2080 0921 8181 0020  &P.ÓÐ. ] ..!... 
00000010: 8001 dc64                                ..Üd

JavaScript equivalent:

a.match(/\d/g).length;

Explanation:

Implicitly takes input as a decimal string in a and implicitly outputs the length of the match, which is all the decimal digits in the string. Returns 1 for input of 0.

See Demo.

Bean, 22 bytes

Hexdump:

00000000: 2aa0 1f26 4ccc d3a0 8043 53a0 802d 2043  * .&LÌÓ .CS .- C
00000010: 9125 398b 253a                           .%9.%:

JavaScript equivalent:

with(Math)((log10(abs(A))^0)+1);

Explanation:

Implicitly takes input as a decimal integer in A, takes the absolute value, then log-base-10, XORs with 0, then adds 1.

When A is 0, the returned value of log10() is -Infinity, and -Infinity ^ 0 is 0 in JavaScript, so the returned value for 0 is 1.

See Demo.

\$\endgroup\$
0
\$\begingroup\$

J, 7 bytes

#@":@**

Try it online!

All the solutions I came up with:

0>.@>.10^.>:@(**)
[:#10&#.inv
#@-.&'-'
#@":@**

The first one is math, the second is base conversion, the third takes a string as input, and the shortest takes a number.

The current solution looks like this:

              ┌─ # 
        ┌─ @ ─┴─ ":
  ┌─ @ ─┴─ *       
──┴─ *             

The parent root is a hook between the upper tine and the lower tine. The lower tine (*) is a monad that calculates magnitude. The upper tine is a composition:

         ┌─ # 
   ┌─ @ ─┴─ ":
@ ─┴─ * 

This composes the upper tine (another composition) with *, in this case, a dyad. This is the dyad for multiplication. In the last tine:

   ┌─ # 
@ ─┴─ ":

This calculates format (":) then length (#).

In English form, this is "multiply x times the sign of x, then convert this to a string, then take the length of this string". Basically, abs.toString.length.

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 9 + 1 = 10 bytes

$_=y/-//c

Try it online!

Run with -p (1 byte penalty).

Explanation

$_=y/-//c
            {implicit from -p: for each line of input}
   y/ //    In {the input}, replace
        c     everything except
     -        '-'
$_=         Store number of replacements back in $_
            {implicit from -p: output $_}

We treat the input as a string in order to handle numbers outside the 64-bit range. One interesting trick here is that we don't have to specify what we're replacing the nonhyphens with; we can still count the number of replacements that occur.

The TIO link uses -l in order to let us run the program on multiple data without the newlines between them interfering. If the program only has to run once, we can do without it, so long as there's no final newline on the input.

\$\endgroup\$
0
\$\begingroup\$

C#, 33 bytes

i=>Math.Abs(i).ToString().Length;
\$\endgroup\$
  • \$\begingroup\$ You need to fully qualify Math and you can save bytes by using +"" instead of ToString(). \$\endgroup\$ – TheLethalCoder May 17 '17 at 14:51
0
\$\begingroup\$

MATL, 4 bytes

47>s

Inspired by Martin's Retina answer: count how many characters have code point exceeding 47 (this excludes the minus sign).

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Braingolf, 2 bytes

dl

Explaination:

dl
    Implicit input from command-line args to stack
d   Pop last item from stack, split it into digits, and push each digit to the stack
 l  Push length of stack to the end of the stack
    Implicit output of last item on stack
\$\endgroup\$
0
\$\begingroup\$

Brain-Flak, 48 + 3 ( -a) = 51 bytes

({}[((((()()()()())){}{})){}{}]){(<><>)}{}([]<>)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

REXX 22 Bytes

arg "-" a
say length(a)

Explanation: There is no distinction in Rexx between numbers and strings. The action you perform is what defines the type. The "typing" applies just to that action and can change at any time.

So, here the number (say -20) is treated as a string. The "arg" instruction (short for parse arg) tells Rexx to search for the first "-" and then put everything after it in the variable "a". If "-" is not found then everything goes in "a".

Try it here

REXX functions and instructions

\$\endgroup\$
0
\$\begingroup\$

C#, 28 bytes

s=>s.Replace("-","").Length;
\$\endgroup\$
0
\$\begingroup\$

C#, 24 bytes

n=>$"{n>0?n:-n}".Length;

Inspired from @DomHastings answer.

\$\endgroup\$
0
\$\begingroup\$

Chaincode, 5 bytes

pqL_+

Explanation

pqL_+ print(
    +   succ(
   _      floor(
  L        log_10(
pq           abs(
               input())))))
\$\endgroup\$
0
\$\begingroup\$

C, 43 bytes

f(char*s){printf("%d",strlen(s)-(*s==45));}

Try it online

\$\endgroup\$
0
\$\begingroup\$

Bash, 15 bytes

wc -L<<<${1//-}

Try it online!

Deletes - from input as array, and otputs length of longest line (wc -c returns one char more than length)

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 38 bytes

CREATE PROC l @ BIGINT AS PRINT LEN(@)

Usage:

EXECUTE l @ = 9999999999
\$\endgroup\$
  • \$\begingroup\$ If it's a negative int, it returns an extra number in the result. Probably need to add LEN(ABS(@)) \$\endgroup\$ – phroureo Oct 2 '17 at 22:16
0
\$\begingroup\$

Lua, 26 bytes

Replaces all occurrences of '-' with the empty string '' and get length with #.

print(#(...):gsub('-',''))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MATL, 6 bytes

nGU0<-

Try it online!

Explanation

n     % Implicitly input a string. Push its length
G     % Push input again
U     % Convert to number (floating-point double). Although integers with absolute
      % value exceeding 2^53 cannot be represented exactly, the sign is correct
0<    % Is it negative?
-     % Subtract. Implicitly display
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 9 bytes + 3 for -F flag=12 bytes

say@F-/-/

Run like perl -F -E 'say@F-/-/'. Takes a single number from stdin without a trailing newline. Can add the -l flag at the cost of an extra byte if you would rather have it accept a trailing newline.

The -F flag auto splits stdin into the array @F. In scalar context, @F evaluates to the length of the array, which is the number of characters in $_ (which comes from stdin). /-/ in a numerical context evaluates to 1 if $_ has a minus sign in it or 0 if $_ does not have a minus sign in it, so @F-/-/ evaluates to the number of non-minus sign characters (i.e. the number of digit characters) read from stdin.

\$\endgroup\$
0
\$\begingroup\$

C, 71 38 bytes

  • -33 bytes FelipeNardiBatista

Try Online

f(char*t){return*t?(*t>'-')+f(t+1):0;}
\$\endgroup\$
0
\$\begingroup\$

Swift, 138 bytes

You would think there would be an easier way to do this.

import Foundation;var d:(String)->Int={return $0.trimmingCharacters(in:CharacterSet(charactersIn:"0123456789").inverted).characters.count}

You can try it here

Un-golfed:

import Foundation // Import the Foundation module

var d:(String)->Int={ // Create a closure that takes in a String and returns an Int

    return // Return the following

    $0.trimmingCharacters(in:  // Removes all characters in the following CharacterSet

        CharacterSet(charactersIn:"0123456789").inverted // Create a CharacterSet with all characters that are not digits

    ).characters.count // Get the length of the resulting String
}
\$\endgroup\$
0
\$\begingroup\$

Axiom, 23 Bytes

f(x)==#(abs(x)::String)
\$\endgroup\$
0
\$\begingroup\$

awk, 22 bytes

$1=length($1<0?-$1:$1)

Try it online!

All test cases

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save one byte by using sqrt($1^2) to take the absolute value. TIO. \$\endgroup\$ – Chris May 3 '18 at 2:52
0
\$\begingroup\$

JavaScript, 23 bytes

x=>`${x<0?-x:x}`.length
\$\endgroup\$
0
\$\begingroup\$

Jelly, 5 bytes

A‘l⁵Ċ

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.