C # converts to a string and retains the decimal place when the double is large and the number of decimal digits is too high

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.