C# 小數(shù)位數(shù)保留的方法集錦
更新時(shí)間:2008年12月30日 20:42:35 作者:
c#下關(guān)于小數(shù)位數(shù)的一些實(shí)現(xiàn)方法集合,方便對(duì)c#小數(shù)位數(shù)控制的朋友。
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.NumberDecimalDigits =intDecLength; //要設(shè)定的小數(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位四舍五入 (不錯(cuò))
double s=0.55555;
result=s.ToString("#0.00");//點(diǎn)后面幾個(gè)0就保留幾位
C#下如果顯示保留小數(shù)位數(shù),及百分號(hào)的解決方法:
1、用NumberFormatInfo類來(lái)解決:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.PercentDecimalDigits = 2;//小數(shù)點(diǎn)保留幾位數(shù).
provider.PercentPositivePattern = 2;//百分號(hào)出現(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ù)位數(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位四舍五入 (不錯(cuò))
double s=0.55555;
result=s.ToString("#0.00");//點(diǎn)后面幾個(gè)0就保留幾位
C#下如果顯示保留小數(shù)位數(shù),及百分號(hào)的解決方法:
1、用NumberFormatInfo類來(lái)解決:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.PercentDecimalDigits = 2;//小數(shù)點(diǎn)保留幾位數(shù).
provider.PercentPositivePattern = 2;//百分號(hào)出現(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精度計(jì)算代碼 java指定精確小數(shù)位
- .net decimal保留指定的小數(shù)位數(shù)(不四舍五入)
- python通過(guò)floor函數(shù)舍棄小數(shù)位的方法
- 用js格式化金額可設(shè)置保留的小數(shù)位數(shù)
- PHP四舍五入精確小數(shù)位及取整
- java小數(shù)位的例子
- javascript中的toFixed固定小數(shù)位數(shù) 簡(jiǎn)單實(shí)例分享
- 關(guān)于數(shù)據(jù)庫(kù)中保留小數(shù)位的問(wèn)題
- C# double和decimal數(shù)據(jù)類型以截?cái)嗟姆绞奖A糁付ǖ男?shù)位數(shù)
- jquery精度計(jì)算代碼 jquery指定精確小數(shù)位
相關(guān)文章
ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理(5)
這篇文章主要介紹了ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09asp.net中使用自定義控件的方式實(shí)現(xiàn)一個(gè)分頁(yè)控件的代碼
在web開(kāi)發(fā)中,常常需要顯示一些數(shù)據(jù),而為了方便排版及瀏覽,我們只需要顯示所有記錄中的一部分。一般情況下,我們采用分頁(yè)來(lái)實(shí)現(xiàn)這個(gè)需求2012-10-10.net開(kāi)發(fā)微信公眾平臺(tái)實(shí)例教程
這篇文章主要介紹了.net開(kāi)發(fā)微信公眾平臺(tái)的方法,對(duì)微信公眾平臺(tái)開(kāi)發(fā)的原理與相應(yīng)的.net實(shí)現(xiàn)方法都做了較為詳細(xì)的講述,非常實(shí)用,需要的朋友可以參考下2014-10-10ASP.NET 網(wǎng)站開(kāi)發(fā)中常用到的廣告效果代碼
在ASP.NET項(xiàng)目開(kāi)發(fā)中,會(huì)被要求添加廣告,有翻屏效果、有廣告輪流顯示、飄浮廣告、左側(cè)廣告、右側(cè)廣告等。2010-04-04最鋒利的Visual Studio Web開(kāi)發(fā)工具擴(kuò)展:Web Essentials使用詳解
Web Essentials是目前為止見(jiàn)過(guò)的最好用的VS擴(kuò)展工具了,具體功能請(qǐng)待我一一道來(lái)。2016-06-06