四舍五入整数的10最接近的倍数

问题描述:

我试图找出如何一轮涨价 - 两种方式。例如:

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?

我只想创造了几个方法;

I would just create a couple methods;

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

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

模给我们的余数,在围捕 10的情况下 - R的带你到最近的十分之一,为本轮下跌你刚才减河pretty直线前进。

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.