如何围绕一个数字

ANP*_*ANP 10 c#

我有变量像float num =(x/y); 当num给出结果如34.443时,我需要对结果进行舍入.那么如何在c#中做到这一点?

Qua*_*ter 25

使用Math.Ceiling:

返回大于或等于指定数字的最小整数

请注意,这适用于双精度数,因此如果您需要浮点数(或整数),则需要进行强制转换.

float num = (float)Math.Ceiling(x/y);
Run Code Online (Sandbox Code Playgroud)

  • 如果`x`和`y`都是整数,它们将被截断. (2认同)

Ven*_*kat 5

 float num = (x/y);
 float roundedValue = (float)Math.Round(num, 2);
Run Code Online (Sandbox Code Playgroud)

如果我们使用Math.Round函数,我们可以指定没有要舍入的地方.