Get the App
SLTechnology News&Howtos  ›  Development  › 

C # retains decimal places instances

Shulou Source: shulou.com Published: 2022-06-03 12:37:36 10月02日 Update

This article mainly introduces "C# retains decimal places instances". In daily operation, I believe that many people have doubts about keeping decimal places instances in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "C# keeping decimal places instances"! Next, please follow the editor to study!

1. A simple example

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo (); provider.NumberDecimalDigits = intDecLength; / / the number of decimal places to be set double strCashAmt=Convert.ToDouble (this.txtCashAmt.Text); / / first convert the value in the control to double this.txtCashAmt.Text = strCashAmt.ToString ("N", provider); / / then format the decimal places with the ToString function

2.C# retains N decimal places and is rounded.

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

3.C# retains decimal N place rounding

Math.Round (0.55555)

Math.Round (0.55555)

4C# retains N decimal places to be rounded

Double dbdata = 0.55555; string str1 = dbdata.ToString ("f2"); / / fN reserves N bits, rounded

5.C# retains decimal N place rounding

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

6. C # keeps decimal N places rounded

Double slots 0.55555; result=s.ToString ("# 0.00"); / / keep a few zeros after clicking

C # preserves the number of decimal places and the solution to the percent sign:

1. Use NumberFormatInfo class to solve the problem:

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo (); provider.PercentDecimalDigits = 2 beat / keep a few places in the decimal point. Where does the provider.PercentPositivePattern = 2% sign appear? Double result = (double) 1 / 3 racket / be sure to use double type. Response.Write (result.ToString ("P", provider))

2. Using 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.0000 ") +"% ";} return rValue } string str1 = String.Format ("{0:N1}", 56789); / / result: 56789.0 string str2 = String.Format ("{0:N2}", 56789); / / result: 56789.00 string str3 = String.Format ("{0:N3}", 56789); / / result: 56789.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.0). ToString ("#. # #"); / / result: 567 this is the end of the study on "C# keeps decimal places". I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Decimal decimal rounded rounded decimal instance learning method more percentage percent help practical next example decimal point control article format Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker Shulou Tech Info MariaDB Redmi