Amb*_*jKN 5 c# floating-point rounding
我正在使用浮点数,我想获得小数点数而不进行任何舍入.
对于Eg.float x = 12.6789如果我想要最多2个小数点,那么我应该得到(x = 12.67)和NOT(x = 12.68),这在舍入发生时会发生.
Plz建议哪种方法最好.
你应该能够使用Math.Truncate():
decimal x = 12.6789m;
x = Math.Truncate(x * 100) / 100; //This will output 12.67
Run Code Online (Sandbox Code Playgroud)