C # reserved decimal Places collection of methods

Source: Internet
Author: User
Tags reserved

ASP tutorial. NET C # A collection of methods for preserving 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%";
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: 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.