Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将整数舍入到10的最近倍数_C#_Currency_Rounding - Fatal编程技术网

C# 将整数舍入到10的最近倍数

C# 将整数舍入到10的最近倍数,c#,currency,rounding,C#,Currency,Rounding,我正试图弄清楚如何使价格四舍五入——两种方式都有。例如: Round down 43 becomes 40 143 becomes 140 1433 becomes 1430 Round up 43 becomes 50 143 becomes 150 1433 becomes 1440 我的情况是,我有一个价格范围,比如: £143 - £193 其中我想展示为: £140 - £200 因为它看起来干净多了 关于如何实现这一点,有什么想法吗?我只想创建几个方法 int RoundUp

我正试图弄清楚如何使价格四舍五入——两种方式都有。例如:

Round down
43 becomes 40
143 becomes 140
1433 becomes 1430

Round up
43 becomes 50
143 becomes 150
1433 becomes 1440
我的情况是,我有一个价格范围,比如:

£143 - £193
其中我想展示为:

£140 - £200
因为它看起来干净多了


关于如何实现这一点,有什么想法吗?

我只想创建几个方法

int RoundUp(int toRound)
{
     if (toRound % 10 == 0) return toRound;
     return (10 - toRound % 10) + toRound;
}

int RoundDown(int toRound)
{
    return toRound - toRound % 10;
}

模给了我们余数,如果向上取整
10-r
将你带到最接近的十分之一,向下取整你只需减去r。非常简单。

将数字除以10

number = number / 10;
Math.Ceiling(number);//round up
Math.Round(number);//round down
然后乘以10

number = number * 10;

此代码四舍五入到10的最接近倍数:

int RoundNum(int num)
{
     int rem = num % 10;
     return rem >= 5 ? (num - rem + 10) : (num - rem);
}
非常简单的用法:

Console.WriteLine(RoundNum(143)); // prints 140
Console.WriteLine(RoundNum(193)); // prints 190

您不需要使用模数(%)或浮点

这项工作:

public static int RoundUp(int value)
{
    return 10*((value + 9)/10);
}

public static int RoundDown(int value)
{
    return 10*(value/10);
}

将一个数四舍五入到另一个数的倍数的一般方法,即四舍五入

用于整数

int RoundNum(int num, int step)
{
    if (num >= 0)
        return ((num + (step / 2)) / step) * step;
    else
        return ((num - (step / 2)) / step) * step;
}
用于浮动

float RoundNum(float num, float step)
{
    if (num >= 0)
        return floor((num + step / 2) / step) * step;
    else
        return ceil((num - step / 2) / step) * step;
}
我知道有些部分可能看起来违反直觉或不是很优化。我尝试将(num+step/2)强制转换为int,但对于负浮点(
(int)-12.0000=-11
等等)给出了错误的结果。总之,我测试了几个案例:

  • 四舍五入到步骤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

我试过数学。FloorFloor是floor,是double。除以10,取整,乘以10根据规范给出1433的错误答案。根据规范给出1433的错误答案可能无法给出非常大的数字(例如Int32.MaxValue)所需的结果。是的,对于大于(Int32.MaxValue-10)的数字,取整将失败。不过,不要认为这是英镑价格的问题。无论如何,不可能对这些数字进行四舍五入(除非返回long)。这对我很有效,我喜欢它不使用模数。等价于Go:func-RoundUp(v-int)int{return 10*((v+9)/10)}func-RoundDown(v-int)int{return 10*(v/10)}。
RoundUp
被破坏。如果你通过了
20
,它将被四舍五入到
30
,这很可能不是你想要的。您可以执行条件测试(
如果((toRound%10)==0)返回toRound;
)或使用无条件舍入,例如
返回((toRound+9)/10)*10注意,上面的逻辑只适用于正数(正如我刚刚发现的)。如果你在价格计算或其他方面有否定的地方,请改为
Math.Abs(toRound)%10
。正如在你答案的第一条评论中所述,
RoundUp(20)
返回
30
@evanmdonnal,这是我听过的最愚蠢的论点。若它并没有被定义为意料之外的东西,那个么它应该遵循惯例。四舍五入到最接近的1:四舍五入(0)=>0,四舍五入(0.5)=>1,四舍五入(1)=>1。最后一个是关键,将已经四舍五入到最近的值四舍五入不会改变它。事实上,许多人都留下了相同的评论,认为这是一种意外的行为,这应该是一个线索给你。我建议其他人使用下面的答案,因为它工作得更好。虽然这个代码片段可能是解决方案,包括一个解释确实有助于提高您的文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
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;
        }