<分区>
Possible Duplicate:
Round a double to 2 significant figures after decimal point
我需要最多 N 个小数,不多了,但我不想要尾随零。例如,如果 N = 2 那么
15.352
15.355
15.3
15
应该成为(分别)
15.35
15.36
15.3
15
<分区>
Possible Duplicate:
Round a double to 2 significant figures after decimal point
我需要最多 N 个小数,不多了,但我不想要尾随零。例如,如果 N = 2 那么
15.352
15.355
15.3
15
应该成为(分别)
15.35
15.36
15.3
15
最佳答案
试试 Math.Round(value, 2).ToString()
Math.Round(15.352, 2).ToString(); //15.35
Math.Round(15.355, 2).ToString(); //15.36
Math.Round(15.3, 2).ToString(); //15.3
Math.Round(15.0, 2).ToString(); //15
round 的第二个参数用于指定要舍入到多少个小数位。默认情况下它会四舍五入。
关于c# - 将double转换为字符串时,如何在C#中将其四舍五入到小数点后N位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8277094/