10
\$\begingroup\$

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 
\$\endgroup\$
6
  • \$\begingroup\$ related but a) has no answers and b) is about calculating the answer to an expression \$\endgroup\$ Commented Nov 2, 2016 at 20:46
  • \$\begingroup\$ Related \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Nov 3, 2016 at 1:45

5 Answers 5

4
\$\begingroup\$

Retina, 29 27 bytes

Saved 2 bytes thanks to @MartinEnder

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

Try it online! | Test suite

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

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 
\$\endgroup\$
0
1
\$\begingroup\$

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.

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

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 dropWhile(48==) //remove leading zeros reverse //and reverse again ) dropWhile(48==) //remove leading zeros size //return the length 
\$\endgroup\$
0
\$\begingroup\$

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! 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" 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.