In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.