使用java进行四舍五入操作有四种方法:
公共统计双精度//向上舍入
第一个: ceil是天花板的意思,表示向上调整。 测试:
系统. out.println (math.ceil (1.01 );
系统. out.println (math.ceil (-1.01 );
系统. out.println (math.ceil (1.5 );
系统. out.println (math.ceil (-1.5 );
输出结果:
2.0
-1.0
2.0
-1.0
第二个: floor是地板的意思,表示向下整理。 测试:
系统. out.println (math.floor (1.01 );
输出: 1.0
第三,round执行的是数学四舍五入执行。 通过查看源代码,可以看到它与floor方法的关系。 测试:
第四个:最有趣的是返回最接近参数的整数,如果两个数一样接近,则返回偶数。 1 )有两种特殊情况:如果参数本身是整数,则返回其本身。
2 )如果不是数字、无限大或正负0,结果就是本身。
returnsthedoublevaluethatisclosestinvaluetotheargumentandisequaltoamathematicalinteger.iftwodoublevaluesthataremathematicalical ose,theresultistheintegervaluethatiseven.special cases 3360
iftheargumentvalueisalreadyequaltoamathematicalinteger,thentheresultisthesameastheargument。
iftheargumentisnanoraninfinityorpositivezeroornegativezero,thentheresultisthesameastheargument。
参数: a双精度值。
returns 3360 theclosestfloating-pointvaluetoathatisequaltoamathematicalinteger。