Show a number with specified number of significant digits

4.4k views Asked by At

I use the following function to convert a number to a string for display purposes (don't use scientific notation, don't use a trailing dot, round as specified):

(* Show Number. Convert to string w/ no trailing dot. Round to the nearest r. *)
Unprotect[Round];   Round[x_,0] := x;   Protect[Round];
shn[x_, r_:0] := StringReplace[
  ToString@NumberForm[Round[N@x,r], ExponentFunction->(Null&)], re@"\\.$"->""]

(Note that re is an alias for RegularExpression.)

That's been serving me well for years. But sometimes I don't want to specify the number of digits to round to, rather I want to specify a number of significant figures. For example, 123.456 should display as 123.5 but 0.00123456 should display as 0.001235.

To get really fancy, I might want to specify significant digits both before and after the decimal point. For example, I might want .789 to display as 0.8 but 789.0 to display as 789 rather than 800.

Do you have a handy utility function for this sort of thing, or suggestions for generalizing my function above?

Related: Suppressing a trailing "." in numerical output from Mathematica

UPDATE: I tried asking a general version of this question here:
https://stackoverflow.com/questions/5627185/displaying-numbers-to-non-technical-users

3

There are 3 answers

1
JohnPS On

This may not be the complete answer (you need to convert from/to string), but this function takes arguments a number x and significant figures sig wanted. The number of digits it keeps is the maximum of sig or the number of digits to the left of the decimal.

A[x_,sig_]:=NumberForm[x, Max[Last[RealDigits[x]], sig]]

RealDigits

3
Mr.Wizard On

dreeves, I think I finally understand what you want, and you already had it, pretty much. If not, please try again to explain what I am missing.

shn2[x_, r_: 0] := 
 StringReplace[
  ToString@NumberForm[x, r, ExponentFunction -> (Null &)], 
  RegularExpression@"\\.0*$" -> ""]

Testing:

shn2[#, 4] & /@ {123.456, 0.00123456}
shn2[#, {3, 1}] & /@ {789.0, 0.789}
shn2[#, {10, 2}] & /@ {0.1234, 1234.}
shn2[#, {4, 1}] & /@ {12.34, 1234.56}

Out[1]= {"123.5", "0.001235"}

Out[2]= {"789", "0.8"}

Out[3]= {"0.12", "1234"}

Out[4]= {"12.3", "1235"}
6
dreeves On

Here's a possible generalization of my original function. (I've determined that it's not equivalent to Mr Wizard's solution but I'm not sure yet which I think is better.)

re = RegularExpression;

(* Show Number. Convert to string w/ no trailing dot. Use at most d significant
   figures after the decimal point. Target t significant figures total (clipped 
   to be at least i and at most i+d, where i is the number of digits in integer 
   part of x). *)
shn[x_, d_:5, t_:16] := ToString[x]
shn[x_?NumericQ, d_:5, t_:16] := With[{i= IntegerLength@IntegerPart@x},
  StringReplace[ToString@NumberForm[N@x, Clip[t, {i,i+d}],
                                    ExponentFunction->(Null&)],
                re@"\\.$"->""]]

Testing:

Here we specify 4 significant digits, but never dropping any to the left of the decimal point and never using more than 2 significant digits to the right of the decimal point.

(# -> shn[#, 2, 4])& /@ 
  {123456, 1234.4567, 123.456, 12.345, 1.234, 1.0001, 0.123, .0001234}

{  123456 -> "123456", 
 1234.456 -> "1234", 
  123.456 -> "123.5"
   12.345 -> "12.35", 
    1.234 -> "1.23", 
   1.0001 -> "1", 
    0.123 -> "0.12", 
0.0001234 -> "0.00012" }