四舍五入到最接近的 100 [英] Rounding to the closest 100

查看:66
本文介绍了四舍五入到最接近的 100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整数四舍五入到最接近的 100?例如,497 将舍入为 500,98 将舍入为 100,1423 将舍入为 1400.

How do you round an integer to the closest 100? For example 497 would round to 500, 98 round to 100, 1423 round to 1400.

推荐答案

我会除以 100,然后再乘以:

I'd divide by 100, round, and then multiply again:

int initial = ...;
int rounded = (int) Math.round(initial/100.0) * 100;

注意要除以 100.0 而不是 100,所以你用浮点运算进行除法.

Note to divide by 100.0 and not 100, so you do the division in floating point arithmetic.

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

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