C# 如何将小数点后的数字四舍五入

C# 如何将小数点后的数字四舍五入,c#,C#,我想把小数点后的一个数字四舍五入为c#中的两个数字 ex:输入:100.175 输出:100.17 input:100.176 output:100.18 (as the number after two digits is above 5,rounding up to 6) 提前感谢试试这个: decimal num = 100.176M; string strNum = num.ToString("N2"); 以下是您需要的: result = Math.Round(MyNum, 2);

我想把小数点后的一个数字四舍五入为c#中的两个数字

ex:输入:100.175 输出:100.17

input:100.176
output:100.18 (as the number after two digits is above 5,rounding up to 6)
提前感谢

试试这个:

decimal num = 100.176M;
string strNum = num.ToString("N2");
以下是您需要的:

result = Math.Round(MyNum, 2);

虽然应该很清楚,但第一个参数是要舍入的数字,第二个参数是您想要的小数位数。

您看过
数学.round
?(您可能需要的是
decimal
类型,而不是
double
类型。)Math.Round有一个重载,您可以在其中指定如何处理中点舍入。对于过载和中点零件。