Given a number as input, determine how many significant figures it has. This number will should be taken as a string because you have to do some special formatting. You'll see what I mean soon (I think).

A digit is a sig-fig if at least one of the following apply:

  • Non-zero digits are always significant.
  • Any zeros between two significant digits are significant.
  • final zero or trailing zeros in the decimal portion only are significant.
  • all digits are significant if nothing follows the decimal place.
  • when there are only zeroes, all but the last zero are considered leading zeroes


A string or string array of the number. It might have a decimal point at the end without a digit after it. It might not have a decimal point at all.


How many sig-figs there are.


1.240 -> 4
0. -> 1
83900 -> 3
83900.0 -> 6
0.025 -> 2
0.0250 -> 3
2.5 -> 2
970. -> 3
0.00 -> 1
  • \$\begingroup\$ related but a) has no answers and b) is about calculating the answer to an expression \$\endgroup\$
    – Daniel
    Nov 2, 2016 at 20:46
  • \$\begingroup\$ Related \$\endgroup\$
    – Adnan
    Nov 2, 2016 at 21:00
  • \$\begingroup\$ You might want to mention explicitly somewhere that if there are only zeros then all but the last zero are considered leading digits (as opposed to all but the first zero being considered trailing digits). \$\endgroup\$ Nov 2, 2016 at 22:14
  • \$\begingroup\$ Why does 0.00 -> 1 ? Aren't the two zeros following the decimal point significant (according to "final zero or trailing zeros in the decimal portion only are significant"). \$\endgroup\$
    – Penguino
    Nov 2, 2016 at 22:58
  • \$\begingroup\$ @Penguino, as Martin Ender correctly said, if there are only 0s, all but the last digit are considered leading zeroes \$\endgroup\$
    – Daniel
    Nov 3, 2016 at 1:45

5 Answers 5


Retina, 29 27 bytes

Saved 2 bytes thanks to @MartinEnder


Try it online! | Test suite


05AB1E, 11 10 bytes


Try it online! or as a Test suite


D            # duplicate
 .ïi  }      # if it is an integer
    0Ü       # remove trailing zeroes
       þ     # remove the "." if present
        ï    # convert to int
         g   # length

Batch, 204 202 bytes

@if %s:.=%%s:~-1%==%s%0 set s=%s:~,-1%&goto t
@set s=%s:.=%
@if not %s%==0 if %s:~,1%==0 set s=%s:~1%&goto l
@set n=0
@if not "%s%"=="" set/an+=1&set s=%s:~1%&goto n
@echo %n%

Takes input on STDIN. Works by removing trailing zeros if the number does not contain a ., then removing the . and leading zeros, unless there are only zeros, in which case it leaves one zero. Finally it takes the length of the remaining string.

  • \$\begingroup\$ I've never seen so many %s in my entire life :O \$\endgroup\$
    – user41805
    Nov 4, 2016 at 19:39
  • 1
    \$\begingroup\$ @KritixiLithos I've managed 16 on one line before: codegolf.stackexchange.com/a/86608/17602 \$\endgroup\$
    – Neil
    Nov 4, 2016 at 20:16

Scala, 90 bytes

& =>(if(&contains 46)&filter(46!=)else&.reverse dropWhile(48==)reverse)dropWhile(48==)size


& =>               //define an anonymous function with a parameter called &
  if(&contains 46) //if & contains a '.'
    &filter(46!=)    //remove all periods
  else             //else
    &.reverse        //reverse the string
    dropWhile(48==)  //remove leading zeros
    reverse          //and reverse again
  dropWhile(48==) //remove leading zeros
  size            //return the length

C#6, 163 bytes

using System.Linq;
int a(string s)=>System.Math.Max(s.Contains('.')?(s[0]<'1'?s.SkipWhile(x=>x<'1').Count():s.Length-1):s.Reverse().SkipWhile(x=>x<'1').Count(),1);


int a(string s)=>                                  
        s.Contains('.')                                // Has decimal place?
            ?(                                         // Has decimal place!
                s[0]<'1'                               // Start with '0' or '.'?
                    ?s.SkipWhile(x=>x<'1').Count()     // Yes: Skip leading '0' and '.', then count rest... But gives 0 for "0." and "0.00"
                    :s.Length-1)                       // No: Count length without decimal place
            :s.Reverse().SkipWhile(x=>x<'1').Count()   // No decimal place: Skip trailing '0' and count rest
    ,1);                                               // For "0." and "0.00"

Your Answer

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

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