C # digit formatting

Source: Internet
Author: User

int str = 1236;MessageBox.Show("E=" + str.ToString("E"));

MessageBox.Show("C=" + str.ToString("C"));

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> MessageBox. Show ("D5 =" + str. ToString ("D5 "));

MessageBox.Show("F2=" + str.ToString("F2"));

MessageBox.Show("G2=" + str.ToString("G2"));

MessageBox.Show("N=" + str.ToString("N"));

MessageBox.Show("{0:N}=" + string.Format("{0:N}", str));

1. Separate the numbers with semicolons and specify the digits after the decimal point

String. Format ("{0: N}", 14200): 14,200.00 (default: two digits after the decimal point)

String. Format ("{0: N3}", 14200.2458): 14,200.246 (Auto rounding)

2. Formatting percentage

String. Format ("{0: P}", 0.24583): 24.58% (two decimal places in the percentage are reserved by default)

String. Format ("{0: P1}", 0.24583): 24.6% (Auto rounding)

Note:

String. Format: replaces each Format item in the specified String type with the text equivalent item of the value of the corresponding object.
Format rules. Format: "{N [, M] [: formatString]}", arg1 ,... ArgN, in this format:
1) N is an integer starting from 0, indicating the number of parameters to be formatted
2) M is an optional integer that represents the width of the formatted parameter. If M is a negative number, the formatted value is left aligned. If M is a positive number, the formatted value is right aligned.
3) formatString is another optional parameter, indicating the format code.
ArgN indicates the expression to be formatted, which corresponds to N.
If argN is null, it is replaced by an empty string. If no formatString exists, use the ToString method corresponding to parameter N to format it.




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.