C# 小數(shù)位數(shù)保留的方法集錦
更新時間:2008年12月30日 20:42:35 作者:
c#下關于小數(shù)位數(shù)的一些實現(xiàn)方法集合,方便對c#小數(shù)位數(shù)控制的朋友。
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.NumberDecimalDigits =intDecLength; //要設定的小數(shù)位數(shù)
double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內(nèi)的值轉(zhuǎn)成double
this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函數(shù)格式化小數(shù)位數(shù)
2.保留N位,四舍五入 .
decimal d= decimal.Round(decimal.Parse("0.55555"),2);
3.保留N位四舍五入
Math.Round(0.55555,2)
4,保留N位四舍五入
double dbdata = 0.55555;
string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入
5.保留N位四舍五入
string result = String.Format("{0:N2}", 0.55555);//2位
string result = String.Format("{0:N3}", 0.55555);//3位
6. 保留N位四舍五入 (不錯)
double s=0.55555;
result=s.ToString("#0.00");//點后面幾個0就保留幾位
C#下如果顯示保留小數(shù)位數(shù),及百分號的解決方法:
1、用NumberFormatInfo類來解決:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.PercentDecimalDigits = 2;//小數(shù)點保留幾位數(shù).
provider.PercentPositivePattern = 2;//百分號出現(xiàn)在何處.
double result = (double)1 / 3;//一定要用double類型.
Response.Write(result.ToString("P", provider));
2、用toString方法.:
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.#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
provider.NumberDecimalDigits =intDecLength; //要設定的小數(shù)位數(shù)
double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內(nèi)的值轉(zhuǎn)成double
this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函數(shù)格式化小數(shù)位數(shù)
2.保留N位,四舍五入 .
decimal d= decimal.Round(decimal.Parse("0.55555"),2);
3.保留N位四舍五入
Math.Round(0.55555,2)
4,保留N位四舍五入
double dbdata = 0.55555;
string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入
5.保留N位四舍五入
string result = String.Format("{0:N2}", 0.55555);//2位
string result = String.Format("{0:N3}", 0.55555);//3位
6. 保留N位四舍五入 (不錯)
double s=0.55555;
result=s.ToString("#0.00");//點后面幾個0就保留幾位
C#下如果顯示保留小數(shù)位數(shù),及百分號的解決方法:
1、用NumberFormatInfo類來解決:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.PercentDecimalDigits = 2;//小數(shù)點保留幾位數(shù).
provider.PercentPositivePattern = 2;//百分號出現(xiàn)在何處.
double result = (double)1 / 3;//一定要用double類型.
Response.Write(result.ToString("P", provider));
2、用toString方法.:
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.#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
您可能感興趣的文章:
- java精度計算代碼 java指定精確小數(shù)位
- .net decimal保留指定的小數(shù)位數(shù)(不四舍五入)
- python通過floor函數(shù)舍棄小數(shù)位的方法
- 用js格式化金額可設置保留的小數(shù)位數(shù)
- PHP四舍五入精確小數(shù)位及取整
- java小數(shù)位的例子
- javascript中的toFixed固定小數(shù)位數(shù) 簡單實例分享
- 關于數(shù)據(jù)庫中保留小數(shù)位的問題
- C# double和decimal數(shù)據(jù)類型以截斷的方式保留指定的小數(shù)位數(shù)
- jquery精度計算代碼 jquery指定精確小數(shù)位
相關文章
ASP.NET?MVC5實現(xiàn)文件上傳與地址變化處理(5)
這篇文章主要介紹了ASP.NET?MVC5實現(xiàn)文件上傳與地址變化處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09asp.net中使用自定義控件的方式實現(xiàn)一個分頁控件的代碼
在web開發(fā)中,常常需要顯示一些數(shù)據(jù),而為了方便排版及瀏覽,我們只需要顯示所有記錄中的一部分。一般情況下,我們采用分頁來實現(xiàn)這個需求2012-10-10ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼
在ASP.NET項目開發(fā)中,會被要求添加廣告,有翻屏效果、有廣告輪流顯示、飄浮廣告、左側(cè)廣告、右側(cè)廣告等。2010-04-04最鋒利的Visual Studio Web開發(fā)工具擴展:Web Essentials使用詳解
Web Essentials是目前為止見過的最好用的VS擴展工具了,具體功能請待我一一道來。2016-06-06