Show number with indicated number of significant digits

I use the following function to convert a number to a string for display purposes (do not use scientific notation, do not use the endpoint rounded as indicated):

(* 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 this reis an alias for RegularExpression.)

It has served me well for years. But sometimes I do not want to indicate the number of digits to round to, rather, I want to indicate a series of significant digits. For example, 123.456 should display as 123.5, but 0.00123456 should display as 0.001235.

To get a true fantasy, I can indicate significant numbers 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, not 800.

Do you have a handy utility function for these kinds of things or suggestions for generalizing my function above?

Related: Suppression of trailing "" in numerical output from Mathematica

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

+3
source share
3 answers

dreeves, I think that I finally understand what you want, and you already had it, to a large extent. If not, 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"}
+5
source

( / ), x sig. , , - sig .

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

RealDigits

+1

A generalization of my original function is possible here. (I decided that this is not equivalent to Mr. Master’s decision, but I’m still not sure which, in my opinion, 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 indicate 4 significant digits, but never discard them to the left of the decimal point and never use more than two 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" }
0
source

Source: https://habr.com/ru/post/1796586/


All Articles