简体   繁体   中英

Format a large double to show four significant digits

I have a large double, for example 345533345.8993 . But, the double is calculated with measured values who have an accuracy of only 4 digits. Hence, in my result I want to show the number of digits that are guaranteed, something like 3455 * 10^5 .

I can write something myself, but I'm looking for a standard way to do this. Is there a way to render this significant measured number?

double large = 345533345.8993;
Console.WriteLine("large double showing four sig figs: {0:####E0}", large);

displays 3455E5

You can use number .ToString("G4") where "G" is a "General number format" returning "the most compact of either fixed-point or scientific notation" and "4" is the number of significant digits ( MSDN ).

345533345.8993.ToString("G4") returns "3.455E+08".

There's also the exponential ("E") format specifier and various other possibilities. See MSDN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM