<分区>
Possible Duplicate:
C# Double - ToString() formatting with two decimal places but no rounding
我使用的是 float ,我想在不执行任何舍入的情况下获取小数点数。
例如。 float x = 12.6789 如果我想要最多 2 个小数点,那么我应该得到 (x = 12.67) 而不是 (x = 12.68),这在四舍五入时发生。
请建议最好的方法。
<分区>
Possible Duplicate:
C# Double - ToString() formatting with two decimal places but no rounding
我使用的是 float ,我想在不执行任何舍入的情况下获取小数点数。
例如。 float x = 12.6789 如果我想要最多 2 个小数点,那么我应该得到 (x = 12.67) 而不是 (x = 12.68),这在四舍五入时发生。
请建议最好的方法。
最佳答案
您应该能够使用 Math.Truncate()为此:
decimal x = 12.6789m;
x = Math.Truncate(x * 100) / 100; //This will output 12.67
关于c# - 在 C# 中,如何在不四舍五入的情况下获得所需的小数点数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6983261/