c# - Make big and small numbers human-readable -
this question has answer here:
- formatting large numbers .net 5 answers
i print small numbers in c# in human friendly way, such as:
30µ
3e-5
or 456.789n
0.000000456789
.
i know of humanize_number() function bsd in c, compatible bit ints, not floats , doubles. there equivalent in c# supports those?
also, should keep amount of precision when displaying numbers, like:
0.003596
should displayed 3.596µ
, not 3.6µ
(or worse, 4µ
).
the possible answer here: formatting large numbers .net adapted negative log10 truncating numbers 1 digit after comma. that's far complete in opinion.
examples of how i'd present things:
3000 3k 3300 3.3k 3333 3.333k 30000 30k 300000 300k 3000000 3m 3000003 3.000003m // or 3m if specify "4 digits precision" 0.253 253m 0.0253 25.3m 0.00253 2.53m -0.253003 -253.003m
i couldn't formulate question find relevant answers in so, if question has been answered, fire away!
try this:
static class extensions { static string[] prefixes= { "f", "a", "p", "n", "μ", "m", string.empty, "k", "m", "g", "t", "p", "e" }; public static string nice(this double x, int significant_digits) { //check special numbers , non-numbers if(double.isinfinity(x)||double.isnan(x)||x==0||significant_digits<=0) { return x.tostring(); } // extract sign deal positive numbers int sign=math.sign(x); x=math.abs(x); // scientific exponent, 10^3, 10^6, ... int sci= x==0? 0 : (int)math.floor(math.log(x, 10)/3)*3; // scale number exponent found x=x*math.pow(10, -sci); // find number of digits left of decimal int dg= x==0? 0 : (int)math.floor(math.log(x, 10))+1; // adjust decimals display int decimals=math.min(significant_digits-dg, 15); // format decimals string fmt=new string('0', decimals); if(sci==0) { //no exponent return string.format("{0}{1:0."+fmt+"}", sign<0?"-":string.empty, math.round(x, decimals)); } // find index prefix. every 3 of sci new index int index=sci/3+6; if(index>=0&&index<prefixes.length) { // prefix return string.format("{0}{1:0."+fmt+"}{2}", sign<0?"-":string.empty, math.round(x, decimals), prefixes[index]); } // 10^exp format return string.format("{0}{1:0."+fmt+"}·10^{2}", sign<0?"-":string.empty, math.round(x, decimals), sci); } // test code static void main(string[] args) { double x=math.pi/10e20; { console.writeline(string.format( "\t{0,20} = {1}", x, x.nice(4))); x*=10; } while(x<=math.pi*10e20); } }
test output 4 significant digits:
3.14159265358979e-19 = 314.2·10^-2 1.5707963267949e-18 = 1.571f 7.85398163397448e-18 = 7.854f 3.92699081698724e-17 = 39.27f 1.96349540849362e-16 = 196.3f 9.8174770424681e-16 = 981.7f 4.90873852123405e-15 = 4.909a 2.45436926061703e-14 = 24.54a 1.22718463030851e-13 = 122.7a 6.13592315154256e-13 = 613.6a 3.06796157577128e-12 = 3.068p 1.53398078788564e-11 = 15.34p 7.6699039394282e-11 = 76.70p 3.8349519697141e-10 = 383.5p 1.91747598485705e-09 = 1.917n 9.58737992428526e-09 = 9.587n 4.79368996214263e-08 = 47.94n 2.39684498107131e-07 = 239.7n 1.19842249053566e-06 = 1.198µ 5.99211245267829e-06 = 5.992µ 2.99605622633914e-05 = 29.96µ 0.000149802811316957 = 149.8µ 0.000749014056584786 = 749.0µ 0.00374507028292393 = 3.745m 0.0187253514146196 = 18.73m 0.0936267570730982 = 93.63m 0.468133785365491 = 468.1m 2.34066892682745 = 2.341 11.7033446341373 = 11.70 58.5167231706864 = 58.52 292.583615853432 = 292.6 1462.91807926716 = 1.463k 7314.5903963358 = 7.315k 36572.951981679 = 36.57k 182864.759908395 = 182.9k 914323.799541975 = 914.3k 4571618.99770987 = 4.572m 22858094.9885494 = 22.86m 114290474.942747 = 114.3m 571452374.713734 = 571.5m 2857261873.56867 = 2.857g 14286309367.8434 = 14.29g 71431546839.2168 = 71.43g 357157734196.084 = 357.2g 1785788670980.42 = 1.786t 8928943354902.1 = 8.929t 44644716774510.5 = 44.64t 223223583872552 = 223.2t 1.11611791936276e+15 = 1.116p 5.58058959681381e+15 = 5.581p 2.79029479840691e+16 = 27.90p 1.39514739920345e+17 = 139.5p 6.97573699601726e+17 = 697.6p 3.48786849800863e+18 = 3.488e 1.74393424900432e+19 = 17.44e 8.71967124502158e+19 = 87.20e 4.35983562251079e+20 = 436.0e 2.1799178112554e+21 = 2.180·10^21
Comments
Post a Comment