C # Decimal Format Usage Summary _c# tutorial

Source: Internet
Author: User

The examples in this article describe C # decimal format usage. Share to everyone for your reference, specific as follows:

1.ToString () method

Double d=12345678.2334;
Console.WriteLine (d.tostring ("F2")); 1234.23
Console.WriteLine (d.tostring ("###,###.00"));//12,345,678.23

2.math.round () method

Math.Round (3.44, 1); Returns 3.4.
Math.Round (3.45, 1); Returns 3.4.
Math.Round (3.46, 1); Returns 3.5.
Math.Round (3.445, 1); Returns 3.4.
Math.Round (3.455, 1); Returns 3.5.
Math.Round (3.465, 1); Returns 3.5.
Math.Round (3.450, 1); Returns 3.4. (complement 0 is invalid)
Math.Round (3.4452, 2); Returns 3.45.
Math.Round (3.4552, 2); Returns 3.46.
Math.Round (3.4652, 2); Returns 3.47.

"Four homes six into five, after five Non-zero into one, five after zero to see the odd couple, five ago for me to give up, five ago for the odd to enter a"

The short one is called "four homes, six, five sets of dolls."

3.double. Parse () method

Double d=1.12345;
D=double. Parse (D.tostring ("0.00")); 1.12

4. Output percent Sign

System.Globalization.NumberFormatInfo Provider = new System.Globalization.NumberFormatInfo ();
Provider. Percentdecimaldigits = 2;//A decimal point to retain several digits.
Provider. Percentpositivepattern = where 1;//percent signs appear.
Double result = (double) 1/3;//Be sure to use double type.
Console.WriteLine (Result. ToString ("P", provider)); 33.33%
//or
Console.WriteLine ((result*100). ToString ("#0. #0") + "%");

5.string.format () method

String str1 = String.Format ("{0:n1}", 56789); result:56,789.0
String str2 = String.Format ("{0:n2}", 56789);//result:56,789.00
string str3 = String.Format ("{0:n3}", 56789); result:56,789.000
String str8 = String.Format ("{0:f1}", 56789);//result:56789.0
string STR9 = String.Format ("{0:f2}", 56789); result:56789.00
String str11 = (56789/100.0). ToString ("#.##"); result:567.89
String str12 = (56789/100). ToString ("#.##"); result:567

Read more about C # Interested readers can view the site topics: "C # form Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Usage Skills summary", "C # Operation Excel Skills Summary", "C # Summary of operational skills in XML files, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming

I hope this article will help you with C # programming.

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.