Round函数不起作用

本文关键字:不起作用 函数 Round | 更新日期: 2023-09-27 17:59:04

我有一个类似45.25214 的浮点值

我想把逗号后面的两个数字相加(或取整)。我试过这个:

sl = sl / count;
Math.Round(sl, 2);

但结果并没有改变;还是那个样子。

Round函数不起作用

Math.Round是一个纯函数,因此您需要使用它的返回值才能使其有用。

sl = sl / count;
sl = Math.Round(sl, 2);

您没有将舍入值分配给原始变量。此外,您应该将其铸造为浮动。

sl = sl / count;
sl = Math.Round(sl,2) as float;