Java 教程 Java round() 方法

topacio · 2022-05-24 23:03:33 · 热度: 24

round() 方法返回一个最接近的 int、long 型值,四舍五入。

round 表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。

语法

该方法有以下几种语法格式:

long round(double d)

int round(float f)

参数

  • d -- double 或 float 的原生数据类型

  • f -- float 原生数据类型

返回值

返回一个最接近的int、long型值,方法会指定返回的数据类型。

实例

实例

public class Test{ public static void main(String args[]){ double d = 100.675; double e = 100.500; float f = 100; float g = 90f; System.out.println(Math.round(d)); System.out.println(Math.round(e)); System.out.println(Math.round(f)); System.out.println(Math.round(g)); } }

编译以上程序,输出结果为:

101
101
100
90

查看更多 Java Number & Math 类 相关内容

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册