C 保留小數字數的方法集錦

2022-01-18 18:44:47 字數 1874 閱讀 3459

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.55555);//2位

string result = string.format("", 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)

double db = (hcount / task) * 100;

if (hcount >= task)

else

return rvalue;

} string str1 = string.format("",56789); //result: 56,789.0

string str2 = string.format("",56789); //result: 56,789.00

string str3 = string.format("",56789); //result: 56,789.000

string str8 = string.format("",56789); //result: 56789.0

string str9 = string.format("",56789); //result: 56789.00

string str11 =(56789 / 100.0).tostring("#.##"); //result: 567.89

string str12 =(56789 / 100).tostring("#.##"); //result: 567

C 保留小數字數的方法集錦

1.system.globalization.numberformatinfo provider new system.globalization.numberformatinfo provider.numberdecimaldigits intdeclength 要設定的小數字數 double s...

C 保留小數字數的方法集錦

c 保留小數字數的方法集錦 1.system.globalization.numberformatinfo provider new system.globalization.numberformatinfo provider.numberdecimaldigits intdeclength 要設定...

C 保留小數字數的方法集錦

1.system.globalization.numberformatinfo provider new system.globalization.numberformatinfo provider.numberdecimaldigits intdeclength 要設定的小數字數 double s...