C # set of methods for retaining decimal places

Source: Internet
Author: User

System. Globalization. NumberFormatInfo provider = new System. Globalization. NumberFormatInfo ();
Provider. NumberDecimalDigits = intDecLength; // number of decimal places to be set
Double strCashAmt=Convert.ToDouble(this.txt CashAmt. Text); // convert the value in the control to double

This.txt CashAmt. Text = strCashAmt. ToString ("N", provider); // use the ToString function to format decimal places.

 
// 2. retain N digits and rounding them.

Decimal d = decimal. Round (decimal. Parse ("0.55555"), 2 );

 
// 3. Keep N digits rounded

Math. Round (0.55555, 2)

 
// 4, keep N digits rounded
Double dbdata = 0.55555;
String str1 = dbdata. ToString ("f2"); // fN retains N bits, rounded

 
// 5. Keep N digits rounded

String result = String. Format ("{0: N2}", 0.55555); // two digits

String result = String. Format ("{0: N3}", 0.55555); // three digits

 
// 6. Keep N digits rounded (good)

Double s = 0.55555;
Result = s. ToString ("#0.00"); // a few digits are retained after a few zeros.

 
// Solution for displaying the number of decimals retained and percentage signs in C:

// 1. Use the NumberFormatInfo class to solve the problem:
System. Globalization. NumberFormatInfo provider = new System. Globalization. NumberFormatInfo ();

Provider. PercentDecimalDigits = 2; // number of decimal places reserved.
Provider. PercentPositivePattern = 2; // Where is the percent sign.
Double result = (double) 1/3; // be sure to use the double type.
Response. Write (result. ToString ("P", provider ));

// 2. Use the toString method .:
Public string getRate (double hcount, double task)
{
String rValue;
String temp = "";

If (task = 0)
{
Task = 1;
}

Double db = (hcount/task) * 100;

If (hcount> = task)
{
RValue = "100% ";
}
Else
{
RValue = db. ToString ("#0. #0") + "% ";
}
Return rValue;
}

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

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.