Summarize C # Keep decimal places

Source: Internet
Author: User

This article to introduce C # keep decimal places, may be a lot of people do not understand C # to keep decimal places, no relationship, after reading this article you must have a lot of harvest, I hope this article can teach you more things.

1. A simple example

    System.Globalization.NumberFormatInfo Provider = new System.Globalization.NumberFormatInfo ();   
    Provider. Numberdecimaldigits =intdeclength; The number of decimal digits to be set   
    strcashamt=convert.todouble (This.txtCashAmt.Text);///First turn the value in the control to double   
     
    This.txtCashAmt.Text = strcashamt.tostring ("N", provider); Using the ToString function to format decimal numbers  


The 2.c# retains the decimal bit n bit, rounded.

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


3.c# Keep decimal bit n-bit rounded

Math.Round (0.55555,2)  


4,c# Keep decimal bit n-bit rounded

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


5.c# Keep decimal bit n-bit rounded

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


6. C # Keep decimal n-bit rounded

Double s=0.55555;   


C # preserves the number of decimal digits, and the solution for the percent semicolon:

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) *;   
 
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.