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

72 Answers 72

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

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.