日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
總結(jié)C#保留小數(shù)位數(shù)

本文向大家介紹C#保留小數(shù)位,可能好多人還不了解C#保留小數(shù)位,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)龍崗免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

1.簡(jiǎn)單的例子

 
 
 
  1. System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();   
  2. provider.NumberDecimalDigits =intDecLength; //要設(shè)定的小數(shù)位數(shù)   
  3. double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內(nèi)的值轉(zhuǎn)成double   
  4.  
  5. this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函數(shù)格式化小數(shù)位數(shù)  

2.C#保留小數(shù)位N位,四舍五入 .

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

3.C#保留小數(shù)位N位四舍五入

Math.Round(0.55555,2)

 
 
 
  1. Math.Round(0.55555,2)  

4,C#保留小數(shù)位N位四舍五入

 
 
 
  1. double dbdata = 0.55555;   
  2. string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入  

5.C#保留小數(shù)位N位四舍五入

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

6. C#保留小數(shù)位N位四舍五入

 
 
 
  1. double s=0.55555;   
  2. result=s.ToString("#0.00");//點(diǎn)后面幾個(gè)0就保留幾位  

C#保留小數(shù)位數(shù),及百分號(hào)的解決方法:

1、用NumberFormatInfo類來解決:

 
 
 
  1. System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();   
  2.  
  3. provider.PercentDecimalDigits = 2;//小數(shù)點(diǎn)保留幾位數(shù).   
  4. provider.PercentPositivePattern = 2;//百分號(hào)出現(xiàn)在何處.   
  5. double result = (double)1 / 3;//一定要用double類型.   
  6. Response.Write(result.ToString("P", provider));  

2、用toString方法.:

 
 
 
  1. public string getRate(double hcount, double task)   
  2. {   
  3. string rValue;   
  4. string temp = "";   
  5.  
  6. if (task == 0)   
  7. {   
  8. task = 1;   
  9. }   
  10.  
  11. double db = (hcount / task) * 100;   
  12.  
  13. if (hcount >= task)   
  14. {   
  15. rValue = "100%";   
  16. }   
  17. else   
  18. {   
  19. rValue = db.ToString("#0.#0") + "%";   
  20. }   
  21. return rValue;   
  22. }   
  23.  
  24. string str1 = String.Format("{0:N1}",56789); //result: 56,789.0   
  25. string str2 = String.Format("{0:N2}",56789); //result: 56,789.00   
  26. string str3 = String.Format("{0:N3}",56789); //result: 56,789.000   
  27. string str8 = String.Format("{0:F1}",56789); //result: 56789.0   
  28. string str9 = String.Format("{0:F2}",56789); //result: 56789.00   
  29. string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89   
  30. string str12 =(56789 / 100).ToString("#.##"); //result: 567   

分享文章:總結(jié)C#保留小數(shù)位數(shù)
文章路徑:http://www.dlmjj.cn/article/djcoesd.html