C # highlights of methods for retaining decimal places

Source: Internet
Author: User

1. 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. Keep N bits, rounded down.

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.

C # solution for displaying the number of decimals retained and percentage signs:

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

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.