数学计算:Math,round四色五入用法

来源:互联网 发布:软件工资 编辑:程序博客网 时间:2024/06/06 19:08

在整个Math类里面可能跟大家使用最密切的就是一个round()方法:public static long round(double a)。

四舍五入代码实现;

package cn.demo;class MyMath{public static double round(double num,int scale){double result = Math.round(num * Math.pow(10, scale))/(double) Math.pow(10,scale);return result;}}public class Test {public static void main(String[] args) throws Exception {System.out.println(MyMath.round(19.3443,2));System.out.println(MyMath.round(19.5443,3));}}
结果:19.34
           19.544

此类操作在日后开发一定会使用到,用的时候直接拿出来使用即可。

0 0