C# 小数位数保留的方法集锦
程序员文章站
2023-11-18 18:41:34
1.system.globalization.numberformatinfo provider = new system.globalization.numberform...
1.system.globalization.numberformatinfo provider = new system.globalization.numberformatinfo();
provider.numberdecimaldigits =intdeclength; //要设定的小数位数
double strcashamt=convert.todouble(this.txtcashamt.text); //先把控件內的值转成double
this.txtcashamt.text = strcashamt.tostring("n",provider); //再利用tostring函数格式化小数位数
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#下如果显示保留小数位数,及百分号的解决方法:
1、用numberformatinfo类来解决:
system.globalization.numberformatinfo provider = new system.globalization.numberformatinfo();
provider.percentdecimaldigits = 2;//小数点保留几位数.
provider.percentpositivepattern = 2;//百分号出现在何处.
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; //要设定的小数位数
double strcashamt=convert.todouble(this.txtcashamt.text); //先把控件內的值转成double
this.txtcashamt.text = strcashamt.tostring("n",provider); //再利用tostring函数格式化小数位数
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#下如果显示保留小数位数,及百分号的解决方法:
1、用numberformatinfo类来解决:
system.globalization.numberformatinfo provider = new system.globalization.numberformatinfo();
provider.percentdecimaldigits = 2;//小数点保留几位数.
provider.percentpositivepattern = 2;//百分号出现在何处.
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