# How many significant figures?

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

## Input

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.

## Output

How many sig-figs there are.

## Examples

1.240 -> 4
0. -> 1
83900 -> 3
83900.0 -> 6
0.025 -> 2
0.0250 -> 3
2.5 -> 2
970. -> 3
0.00 -> 1

• related but a) has no answers and b) is about calculating the answer to an expression – Daniel Nov 2 '16 at 20:46
• Related – Adnan Nov 2 '16 at 21:00
• 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). – Martin Ender Nov 2 '16 at 22:14
• 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"). – Penguino Nov 2 '16 at 22:58
• @Penguino, as Martin Ender correctly said, if there are only 0s, all but the last digit are considered leading zeroes – Daniel Nov 3 '16 at 1:45

# 05AB1E, 11 10 bytes

D.ïi0Ü}þïg


Try it online! or as a Test suite

Explanation

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


# Retina, 29 27 bytes

Saved 2 bytes thanks to @MartinEnder

^0\.0*.|^\d(\d*?)0+$1$1
\d


## Batch, 204 202 bytes

@set/ps=
:t
@if %s:.=%%s:~-1%==%s%0 set s=%s:~,-1%&goto t
@set s=%s:.=%
:l
@if not %s%==0 if %s:~,1%==0 set s=%s:~1%&goto l
@set n=0
:n
@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.

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

# Scala, 90 bytes

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


Explanation:

& =>               //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
reverse          //and reverse again
)
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);


Ungolfed

int a(string s)=>
System.Math.Max(
s.Contains('.')                                // Has decimal place?
?(                                         // Has decimal place!