C # A collection of methods for keeping decimal places

Source: Internet
Author: User

1.system.globalization.numberformatinfo Provider = new System.Globalization.NumberFormatInfo ();
Provider.     Numberdecimaldigits =intdeclength; Number of decimal places to set
Double strcashamt=convert.todouble (This.txtCashAmt.Text); First, turn the values in the controls into double

This.txtCashAmt.Text = strcashamt.tostring ("N", provider); Using the ToString function to format decimal places

2. Keep n bit, rounded.

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

3. Keep N bit rounded

Math.Round (0.55555,2)

4, keep n bit rounded
Double dbdata = 0.55555;
String str1 = Dbdata. ToString ("F2");//fn keep n bit, rounded

5. Keep N bit rounded

string result = String.Format ("{0:n2}", 0.55555);//2 bit

string result = String.Format ("{0:n3}", 0.55555);//3 bits

6. Keep n-bit rounded (yes)

Double s=0.55555;
Result=s.tostring ("#0.00"); a few 0 on the back of the point keep a few

In C #, if you show how to keep the decimal digits, and the percent-semicolon solution:

1, with the NumberFormatInfo class to solve:
System.Globalization.NumberFormatInfo Provider = new System.Globalization.NumberFormatInfo ();

Provider. Percentdecimaldigits = 2;//A decimal point to retain several digits.
Provider. Percentpositivepattern = where 2;//percent signs appear.
Double result = (double) 1/3;//Be sure to use double type.
Response.Write (Result. ToString ("P", provider));

2, with 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.