C#:如何将整数舍入到最接近的1000

fra*_*pet 1 c# math int rounding

我如何舍入(int)使得像(22536)这样的数字等于22000或23000?

我没有在Math类中找到一个特定的方法,Math.Round似乎只将double加倍到最接近的int.

Pat*_*man 10

通过使用模数:

int x = 1500;

int result = x % 1000 >= 500 ? x + 1000 - x % 1000 : x - x % 1000;
Run Code Online (Sandbox Code Playgroud)

x当数千个被剥离时,它检查是否有超过499,然后对其进行舍入.