复制代码 代码如下:
/*
* 测试四舍五入
*/
package com.icer.test;
/**
*
* @author hanbin
*/
public class myround {
public static void main(string[] args) {
double num = 3.23562;
double number = 0;
number = new myround().myround(num,2);
system.out.println("after:" + number);
}
private double myround(double number,int index){
double result = 0;
double temp = math.pow(10, index);
result = math.round(number*temp)/temp;
return result;
}
}
因为java中的round函数在处理完小数之后就去掉了小数位,故先乘100,然后再除以100.0。
保证输出的是小数。否则会被截成整数