C # Double. tostring () C # Save decimal places C # rounding

Source: Internet
Author: User

In C #, we all encounter this situation where data of the double type needs to be formatted (retain N unvalid numbers) or N as decimal places.Double. tostring ("parameter ");. The following describes several common methods.

Double temp = 3.1415926;

(F)Fixed Point:String str1 = temp. tostring ("f1"); // returns the result of rounding a decimal number to 3.1.

(F)Fixed Point:String str2 = temp. tostring ("F2"); // retain two decimal places, rounding the following result and so on: 3.14

(N)Number:String str2 = temp. tostring ("N"); // reserved result: 3.14

(G) General (Default ):String str2 = temp. tostring ("G"); // reserved result: 3.1415926

(P)Percent:String str2 = temp. tostring ("p"); // reserved result: 314.16%

(E)Scientific:String str2 = temp. tostring ("e"); // reserved result E: 3.141593e + 000

(C)Currency:String str2 = temp. tostring ("C"); // reserved result: ¥3.14

Related Article

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.