四舍五入整数的10最接近的倍数 [英] Rounding integers to nearest multiple of 10

查看:1101
本文介绍了四舍五入整数的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.

这篇关于四舍五入整数的10最接近的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆