Today, in C #, you encounter a problem that requires you to convert a double to a string display, requiring you to keep the decimal digits.
Check out the relevant articles on the Internet
Specific as follows:
Double temp=3.1415926;
F Fixed point:string str1=temp.tostring ("F1");//Keep one decimal rounding Result: 3.1
F Fixed point:string str2=temp.tostring ("F2");//reserved Two decimal places, rounding the following analogy results: 3.14
N Number:string str2=temp.tostring ("N");//retention Result: 3.14
(G) General (Default): String Str2=temp.tostring ("G");//retention Result: 3.1415926
P Percent:string str2=temp.tostring ("P");//retention Result: 314.16%
E Scientific:string str2=temp.tostring ("E");//Keep the result e:3.141593e+000
C Currency:string str2=temp.tostring ("C");//retention Result: ¥3.14
For Double temp=0.000000926, none of the above methods work and can be displayed again by turning into decimal format. As shown below:
String str = ((decimal) temp). ToString ();
The final conversion to the decimal format of the display, in some cases still not, will be rounded, for example, double temp=6356911.946127947 when converted to decimal format, the value displayed is 6356911.94612795 Not meet the requirements. In this case, you can use Doubleconverter to convert, as shown below Doubleconvert bc=new Doubleconvert (); string STR=BC. ConvertToString (temp); In this case, the full value is displayed.
C # converts to a string and retains the decimal place when the double is large and the number of decimal digits is too high