java中的舍入值 [英] Round values in java

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

问题描述

我将如何舍入

  1. 1 <值<1.5 到 1.5

  1. 1 < value < 1.5 to 1.5

1.5 <值<2 到 2

1.5 < value < 2 to 2

推荐答案

怎么样

double rounded = Math.ceil(number * 2) / 2;

由于Math.ceil() 已经返回了一个double,所以这里不需要除以2.0d.只要您在可以表示为双精度的整数范围内而不会丢失精度,这将正常工作,但如果您超出该范围,请注意.

Since Math.ceil() already returns a double, no need to divide by 2.0d here. This will work fine as long as you're in the range of integers that can be expressed as doubles without losing precision, but beware if you fall out of that range.

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

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