C # retained two decimal places,

Source: Internet
Author: User

C # retained two decimal places,

Scenario 1:

C # retain 2 decimal places ,. toString ("f2") is true, but if the number is originally three digits after the decimal point, for example, 1.253, it will be converted to 1. 25. can I just keep it to the position where the last digit is not 0?
By default, 2 digits are reserved. If there are three decimal places, 3 digits are retained. If there are four digits, 4 digits are retained.

First, let's talk about the difference between 0 and # In ToString ("0.00:
0 indicates the placeholder. If ToString ("0.00") is used, two decimal places are retained, regardless of the number of decimal places or no decimal places. For example, the result of 1.1234 is 1.12. If it is 1.2, the result will be zero, which is 1.20.
# Indicates that the subsequent values are retained if they are not zero. If they are 0, they are removed. For example, if ToString ("0. #") is set to 1.20, the result is 1.2.


For scenario 1, the default two values are 00. If the other values are larger than two values, they are not zero. You can write as follows:

double dd = 1.2530;string ret = dd.ToString("0.00####"); // 1.253

 

Scenario 2:

According to international practice, sometimes we need to "Rounding" the excess digits ". There are many methods to retain two decimal places using C #. The common summary is as follows:

1. Math. Round (0.333333, 2); // follow the rounding international standards
2. double dbdata = 0.335333; string str1 = String. Format ("{0: F}", dbdata); // retain two digits by default
3. float I = 0.333333; int j = (int) (I * 100); I = j/100;
4. decimal. Round (decimal. Parse ("0.3333333"), 2)
5. private System. Globalization. NumberFormatInfo nfi = new System. Globalization. NumberFormatInfo ();

Float test = 0.333333f;

Nfi. NumberDecimalDigits = 2;

String result = test. ToString ("N", nfi );


6. string result = String. Format ("{0: N2}", Convert. ToDecimal ("0.333333"). ToString ());
7. Convert. ToDecimal ("0.33333333"). ToString ("0.00 ");

 

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.