java四舍五入保留2位小数

java四舍五入保留2位小数 的小坑,附例子分享:java 1.8
RoundingMode.HALF_UP 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class test02 {
	public static void main(String[] args) throws Exception {
		concurrency();
	}

	private static void concurrency() throws Exception {
		Double d1=82.365;
		Double d2=82.3651;
		Double d3=82.265;
		Double d4=82.375;
		Double d5=82.3751;
		Double d6=82.275;
		System.out.println("test:-----------------"+d1+"-----"+d2+"-----"+d3+"------"+d4+"------"+d5+"------"+d6);
		System.out.println("理想结果:-------------"+82.37+"-----"+82.37+"-----"+82.27+"------"+82.38+"------"+82.38+"------"+82.28);

		//写法1,结果不不符合
		DecimalFormat df = new DecimalFormat("#0.00");
		df.setRoundingMode(RoundingMode.HALF_UP);
		System.out.println("RoundingMode.HALF_UP:--"+df.format(d1)+"-----"+df.format(d2)+"-----"+df.format(d3)+"------"+df.format(d4)+"------"+df.format(d5)+"------"+df.format(d6));

		//写法2,结果不不符合
		DecimalFormat df2 = new DecimalFormat("#0.00");
		df2.setRoundingMode(RoundingMode.HALF_EVEN);//RoundingMode.HALF_EVEN  为默认
		System.out.println("RoundingMode.HALF_EVEN:"+df2.format(d1)+"-----"+df2.format(d2)+"-----"+df2.format(d3)+"------"+df2.format(d4)+"------"+df2.format(d5)+"------"+df2.format(d6));

		//写法3,结果不不符合
		DecimalFormat df3 = new DecimalFormat("#0.00");
		df3.setRoundingMode(RoundingMode.UP);
		System.out.println("RoundingMode.UP:-------"+df3.format(d1)+"-----"+df3.format(d2)+"-----"+df3.format(d3)+"------"+df3.format(d4)+"------"+df3.format(d5)+"------"+df3.format(d6));

		//写法4,结果不不符合
		DecimalFormat df4 = new DecimalFormat("#0.00");
		df4.setRoundingMode(RoundingMode.FLOOR);
		System.out.println("RoundingMode.FLOOR:----"+df4.format(d1)+"-----"+df4.format(d2)+"-----"+df4.format(d3)+"------"+df4.format(d4)+"------"+df4.format(d5)+"------"+df4.format(d6));

		//RoundingMode 下的其余 DOWN,CEILING,HALF_DOWN,UNNECESSARY   82.3650 不做test

		NumberFormat nf = NumberFormat.getNumberInstance();
		// 保留两位小数
		nf.setMaximumFractionDigits(2);
		// 如果不需要四舍五入,可以使用RoundingMode.DOWN
		nf.setRoundingMode(RoundingMode.UP);
		System.out.println("NumberFormat format:---"+nf.format(d1)+"-----"+nf.format(d2)+"-----"+nf.format(d3)+"------"+nf.format(d4)+"------"+nf.format(d5)+"------"+nf.format(d6));

		//BigDecimal divide RoundingMode.UP
		System.out.println("BigDecimal divide:-----"+fornum(d1,1,2)+"-----"+fornum(d2,1,2)+"-----"+fornum(d3,1,2)+"------"+fornum(d4,1,2)+"------"+fornum(d5,1,2)+"------"+fornum(d6,1,2));

		//Math.round
		System.out.println("Math.round:------------"+formatDouble1(d1)+"-----"+formatDouble1(d2)+"-----"+formatDouble1(d3)+"------"+formatDouble1(d4)+"------"+formatDouble1(d5)+"------"+formatDouble1(d6));
	}
//scale  保留小数位
	public static Double fornum(Double v1, int v2, int scale) {
		if (v2 != 0) {
			BigDecimal b1 = new BigDecimal(v1);
			BigDecimal b2 = new BigDecimal(Double.toString(v2));
			return b1.divide(b2,scale,RoundingMode.UP).doubleValue();
		} else {
			return 0.00;
		}
	}
	public static double formatDouble1(double d) {
		return (double)Math.round(d*100)/100;
	}
}

结果:
test:------------82.365-----82.3651-----82.265------82.375------82.3751------82.275
理想结果:--------------82.37-----82.37-----82.27------82.38------82.38------82.28
RoundingMode.HALF_UP:--82.36-----82.37-----82.27------82.38------82.38------82.28
RoundingMode.HALF_EVEN:82.36-----82.37-----82.27------82.38------82.38------82.28
RoundingMode.UP:-------82.37-----82.37-----82.27------82.38------82.38------82.28
RoundingMode.FLOOR:----82.36-----82.36-----82.26------82.37------82.37------82.27
NumberFormat format:---82.37-----82.37-----82.27------82.38------82.38------82.28-T
BigDecimal divide:-----82.37-----82.37-----82.27------82.38------82.38------82.28-T
Math.round:------------82.37-----82.37-----82.27------82.38------82.38------82.28-T


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨水0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值