我试图将数字舍入到最低的100.基本上199 - > 100,2021 - > 2000等.我怎样才能在C#程序中实现这一点?
这样做:
private static int RoundDown(int input)
{
return input / 100 * 100;
}
Run Code Online (Sandbox Code Playgroud)
其工作原理如下:
将整数值除以100将得到结果的整数部分.这意味着,如果我们取整数199并除以100,则数学结果为1.99,但因为我们使用整数,所以只保留1.再将该值乘以100将得到100,这是期望的结果.