# Java中高精度问题的bigdecimal解决方式

+关注继续查看

于是，翻了点资料，在有的编程语言中提供了专门的货币类型来处理这种情况，但是Java没有。
四舍五入

public double round(double value){
return Math.round(value*100)/100.0;
}

4.015*100=401.49999999999994

java.text.DecimalFormat也不能解决这个问题：
System.out.println(new java.text.DecimalFormat("0.00").format(4.025));

BigDecimal

BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a
BigDecimal.

Note: the results of this constructor can be somewhat unpredictable. One might
assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal
to .1000000000000000055511151231257827021181583404541015625. This is so because
.1 cannot be represented exactly as a double (or, for that matter, as a binary
fraction of any finite length). Thus, the long value that is being passed in to
the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new
BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is
generally recommended that the (String) constructor be used in preference to
this one.

BigDecimal）再转换为浮点数。你能够忍受这么烦琐的过程吗？下面我们提供一个工具类
Arith来简化操作。它提供以下静态方法，包括加减乘除和四舍五入：
public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)

package com.common.util;
import java.math.BigDecimal;

public final class Arith {
// 默认除法运算精度
private static final int DEF_DIV_SCALE = 2;
// 这个类不能实例化
private Arith() {
}

public static double add(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
}

public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}

public static double mul(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}

public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}

public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}

|
3月前
|

Java之API详解之BigDecimal类的详细解析
Java之API详解之BigDecimal类的详细解析
29 0
|
3月前
|
Java 程序员
Java浮点型变量和BigDecimal的使用
Java浮点型变量和BigDecimal的使用
28 0
|
3月前
|

36 0
|
4月前
|
Java
Java 中大数的处理方案BigInteger和BigDecimal类的使用
Java 中大数的处理方案BigInteger和BigDecimal类的使用
22 0
|
4月前
|

Java中-BigDecimal类型如何进行加减乘除运算？
Java中-BigDecimal类型如何进行加减乘除运算
50 0
|
5月前
|
Java
Java开发篇- 从BigDecimal的divide的异常说起

293 0
|
8月前
|
Java
Java的BigDecimal里方法应该这样用
Java的BigDecimal里方法应该这样用
82 0
|
8月前
|
Java
Java BigDecimal 的舍入模式（RoundingMode）详解
Java BigDecimal 的舍入模式（RoundingMode）详解
179 0
|
9月前
|
Java
Java中BigDecimal保留两位小数
Java中BigDecimal保留两位小数
523 0
|
9月前
|
Java BI
Java BigDecimal 在不设置数精度的情况下，去掉后面的小数点多余的0，有时候就是有这样
BigDecimal 在不设置数精度的情况下，去掉后面的小数点多余的0，有时候就是有这样
205 0