C# 将整数四舍五入到最接近的 10 的倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15154457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding integers to nearest multiple of 10
提问by dhardy
I am trying to figure out how to round prices - both ways. For example:
我想弄清楚如何四舍五入 - 两种方式。例如:
Round down
43 becomes 40
143 becomes 140
1433 becomes 1430
Round up
43 becomes 50
143 becomes 150
1433 becomes 1440
I have the situation where I have a price range of say:
我的情况是,我的价格范围是:
£143 - £193
of which I want to show as:
其中我想显示为:
£140 - £200
as it looks a lot cleaner
因为它看起来干净多了
Any ideas on how I can achieve this?
关于如何实现这一目标的任何想法?
采纳答案by evanmcdonnal
I would just create a couple methods;
我只想创建几个方法;
int RoundUp(int toRound)
{
if (toRound % 10 == 0) return toRound;
return (10 - toRound % 10) + toRound;
}
int RoundDown(int toRound)
{
return toRound - toRound % 10;
}
Modulus gives us the remainder, in the case of rounding up 10 - r
takes you to the nearest tenth, to round down you just subtract r. Pretty straight forward.
模数给我们余数,在四舍五入的情况下,10 - r
你会得到最接近的十分之一,四舍五入你只需减去 r。很直接。
回答by Sharun
Divide the number by 10.
将数字除以 10。
number = number / 10;
Math.Ceiling(number);//round up
Math.Round(number);//round down
Then multiply by 10.
然后乘以10。
number = number * 10;
回答by Parimal Raj
This code rounds to the nearest multiple of 10:
此代码四舍五入到最接近的 10 倍数:
int RoundNum(int num)
{
int rem = num % 10;
return rem >= 5 ? (num - rem + 10) : (num - rem);
}
Very simple usage :
非常简单的用法:
Console.WriteLine(RoundNum(143)); // prints 140
Console.WriteLine(RoundNum(193)); // prints 190
回答by Matthew Watson
You don't need to use modulus (%) or floating point...
您不需要使用模数 (%) 或浮点数...
This works:
这有效:
public static int RoundUp(int value)
{
return 10*((value + 9)/10);
}
public static int RoundDown(int value)
{
return 10*(value/10);
}
回答by Chris Maes
A general method to round a number to a multiple of another number, rounding away from zero.
一种将数字四舍五入为另一个数字的倍数的通用方法,从零四舍五入。
For integer
对于整数
int RoundNum(int num, int step)
{
if (num >= 0)
return ((num + (step / 2)) / step) * step;
else
return ((num - (step / 2)) / step) * step;
}
For float
对于浮动
float RoundNum(float num, float step)
{
if (num >= 0)
return floor((num + step / 2) / step) * step;
else
return ceil((num - step / 2) / step) * step;
}
I know some parts might seem counter-intuitive or not very optimized. I tried casting (num + step / 2) to an int, but this gave wrong results for negative floats ((int) -12.0000 = -11
and such). Anyways these are a few cases I tested:
我知道有些部分可能看起来违反直觉或没有得到很好的优化。我尝试将 (num + step / 2) 转换为 int,但这对于负浮点数((int) -12.0000 = -11
等等)给出了错误的结果。无论如何,这些是我测试过的几个案例:
- any number rounded to step 1 should be itself
- -3 rounded to step 2 = -4
- -2 rounded to step 2 = -2
- 3 rounded to step 2 = 4
- 2 rounded to step 2 = 2
- -2.3 rounded to step 0.2 = -2.4
- -2.4 rounded to step 0.2 = -2.4
- 2.3 rounded to step 0.2 = 2.4
- 2.4 rounded to step 0.2 = 2.4
- 任何四舍五入到第 1 步的数字都应该是它本身
- -3 四舍五入到第 2 步 = -4
- -2 四舍五入到第 2 步 = -2
- 3 四舍五入到第 2 步 = 4
- 2 四舍五入到第 2 步 = 2
- -2.3 四舍五入到步长 0.2 = -2.4
- -2.4 四舍五入到步长 0.2 = -2.4
- 2.3 四舍五入到步长 0.2 = 2.4
- 2.4 四舍五入到步长 0.2 = 2.4
回答by Liakat
public static int Round(int n)
{
// Smaller multiple
int a = (n / 10) * 10;
// Larger multiple
int b = a + 10;
// Return of closest of two
return (n - a > b - n) ? b : a;
}