The go 0 display of decimal in C #

Source: Internet
Author: User

In the recent development, encountered a decimal in the display 0 of the problem, a long time to do a good job, and now briefly introduce a small part of the other online very much

public static string decimaltostring (decimal D)
{
Return d.tostring ("#0. ######");
}

This display is very simple to a few examples to understand, notice the first result, will be rounded

private void Button1_Click (object sender, EventArgs e)
{
decimal D0 = 0.0000006m;
decimal D1 = 0.005000m;
Decimal D2 = 1.00005m;
decimal D3 = 200.00000m;
Decimal d4 = 200.00006m;
Console.WriteLine (Decimaltostring (D0));
Console.WriteLine (decimaltostring (D1));
Console.WriteLine (Decimaltostring (D2));
Console.WriteLine (Decimaltostring (D3));
Console.WriteLine (Decimaltostring (D4));
}

The results are as follows:

0.000001
0.005
1.00005
200
200.00006

The following 0 will be removed, if less than 0 of the number, 0 bits or 0, so that it is more in line with the user's needs of the habit.

Append the description of the two symbols on # and 0:

The following text is taken from: links

5, zero placeholder and digit placeholder

String. The result of Format ("{0:0000.00}", 12394.039) is: 12394.04

String. The result of Format ("{0:0000.00}", 194.039) is: 0194.04

String. Format ("{0:###.##}", 12394.039) result is: 12394.04

String. Format ("{0:####.#}", 194.039) result is: 194

The following explanation is difficult to understand, more test the actual application can be understood.
Zero placeholder:
If the formatted value has a number in the format string where "0" appears, this number is copied to the result string. The position of the leftmost "0" before the decimal point and the rightmost "0" after the decimal point determine the total number range that appears in the result string.
The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, where 0 bits are always removed.

Digit placeholder:
If the formatted value has a number in the format string where "#" appears, the number is copied to the result string. Otherwise, no value is stored at this location in the resulting string.
Note that if "0" is not a valid number, this specifier never displays the "0" character, even if "0" is the only number in the string. If "0" is a valid number in the displayed number, the "0" character is displayed.
The "# #" format string allows the value to be rounded to the nearest digit before the decimal point, where 0 is always removed.

Note: Here is a valid number, so if it is 2.00300 then 2 to 3 between 0 is a valid number, 3 behind the 0 is not a valid number, so it will be removed. The principle is probably this, if there is a supplement can comment or contact me.

The go 0 display of decimal in C #

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.