使用BigDecimal完成小数点后的精确位数的四舍五入

package com.ryan;

import java.math.BigDecimal;

class MyMath {
	public static double round(double num ,int scale) {
		BigDecimal big  = new BigDecimal(num);
		BigDecimal result = big.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);
		return result.doubleValue();
	}
}

public class Test {
	public static void main(String[] args) {
		System.out.println("带小数的四舍五入测试" + MyMath.round(99.4567, 3)); //994.457
	}
}

 

posted @ 2017-07-12 14:37  有梦就能实现  阅读(660)  评论(0编辑  收藏  举报