C\

From , 3 Years ago, written in C#, viewed 220 times.
URL https://pastebin.vip/view/0ec29ebf
  1. //1、
  2. System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
  3. provider.NumberDecimalDigits =intDecLength; //要设定的小数位数
  4. double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值转成double
  5.  
  6. this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函数格式化小数位数
  7.  
  8.  
  9. //2.保留N位,四舍五入 .
  10.  
  11. decimal d= decimal.Round(decimal.Parse("0.55555"),2);
  12.  
  13.  
  14. //3.保留N位四舍五入
  15.  
  16. Math.Round(0.55555,2)
  17.  
  18.  
  19. //4,保留N位四舍五入
  20. double dbdata = 0.55555;
  21. string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入
  22.  
  23.  
  24. //5.保留N位四舍五入
  25.  
  26. string result = String.Format("{0:N2}", 0.55555);//2位
  27.  
  28. string result = String.Format("{0:N3}", 0.55555);//3位
  29.  
  30.  
  31. //6. 保留N位四舍五入 (不错)
  32.  
  33. double s=0.55555;
  34. result=s.ToString("#0.00");//点后面几个0就保留几位
  35.  
  36.  
  37. //C#下如果显示保留小数位数,及百分号的解决方法:
  38.  
  39. //1、用NumberFormatInfo类来解决:
  40. System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
  41.  
  42. provider.PercentDecimalDigits = 2;//小数点保留几位数.
  43. provider.PercentPositivePattern = 2;//百分号出现在何处.
  44. double result = (double)1 / 3;//一定要用double类型.
  45. Response.Write(result.ToString("P", provider));
  46.  
  47. //2、用toString方法.:
  48. public string getRate(double hcount, double task)
  49. {
  50. string rValue;
  51. string temp = "";
  52.  
  53. if (task == 0)
  54. {
  55. task = 1;
  56. }
  57.  
  58. double db = (hcount / task) * 100;
  59.  
  60. if (hcount >= task)
  61. {
  62. rValue = "100%";
  63. }
  64. else
  65. {
  66. rValue = db.ToString("#0.#0") + "%";
  67. }
  68. return rValue;
  69. }
  70.  
  71. string str1 = String.Format("{0:N1}",56789); //result: 56,789.0
  72. string str2 = String.Format("{0:N2}",56789); //result: 56,789.00
  73. string str3 = String.Format("{0:N3}",56789); //result: 56,789.000
  74. string str8 = String.Format("{0:F1}",56789); //result: 56789.0
  75. string str9 = String.Format("{0:F2}",56789); //result: 56789.00
  76. string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
  77. string str12 =(56789 / 100).ToString("#.##"); //result: 567
  78. //csharp/7006

Reply to "C\"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website