32
\$\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\$
0

77 Answers 77

1 2
3
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\$

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\$

T-SQL, 38 bytes

CREATE PROC l @ BIGINT AS PRINT LEN(@)

Usage:

EXECUTE l @ = 9999999999
\$\endgroup\$
1
  • \$\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, 2017 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\$
4
  • \$\begingroup\$ why don't you just return the number? \$\endgroup\$ May 18, 2017 at 10:57
  • \$\begingroup\$ golfed to 38 bytes \$\endgroup\$ May 18, 2017 at 11:05
  • \$\begingroup\$ @FelipeNardiBatista changed now, thx \$\endgroup\$
    – Khaled.K
    May 18, 2017 at 11:19
  • \$\begingroup\$ 35 bytes \$\endgroup\$
    – c--
    Jul 20, 2022 at 14:48
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\$

Desmos, 31 bytes

g=abs(a)
f(a)=floor(log(g+0^g))

Try it on Desmos!

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

Batch, 101 bytes

@set/pi=
@set o=-1&if %i:~,1%==- set o=-2
:l
@set/ao+=1&if %i%. neq . set i=%i:~,-1%&goto:l
@echo %o%

89 bytes

@set/pi=
@if %i% lss 0 set/ai=-%i%
:l
@set/ao+=1&if %i% gtr 9 set/ai/=10&goto:l
@echo %o%

Only works for 32-bit numbers.

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

MathGolf, 2 bytes

±£

Input as integers.

Try it online.

I'm actually surprised builtin £ works on integers. I was expecting I would need an explicit cast, since that's the case with a lot of MathGolf's builtins.

Explanation:

±   # Get the absolute value of the (implicit) input-integer
 £  # Pop and get the length of this integer
    # (after which the entire stack is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Japt, 2 bytes

ìl

Try it

ìl     :Implicit input of integer
ì      :Convert to digit array
 l     :Length
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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