Java BigDecimal差异 [英] Java BigDecimal difference

查看:76
本文介绍了Java BigDecimal差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看是否有人可以解释为什么以下代码可以与valueOf一起使用,但不能与其他人一起使用.

I wanted to see if anyone can explain why the following code works with valueOf but not others.

import java.math.BigDecimal; 
public class Change {
   public static void main(String args[]) {
     double a = 4.00d;
     double b = 3.10d;
     BigDecimal a1 = new BigDecimal(a);
     BigDecimal b1 = new BigDecimal(b);
     BigDecimal diff = a1.subtract(b1);
     System.out.println("Double difference");
     System.out.println(diff);

     float c = 4.00f;
     float d = 3.10f;
     BigDecimal a2 = new BigDecimal(c);
     BigDecimal b2 = new BigDecimal(d);
     BigDecimal diff2 = a2.subtract(b2);
     System.out.println("Float difference");
     System.out.println(diff2);

     System.out.println("Valueof Difference");
     System.out.println(BigDecimal.valueOf(4.00).subtract(BigDecimal.valueOf(3.10)));

   }
 }

输出如下:

>java Change
 Double difference
  0.899999999999999911182158029987476766109466552734375
 Float difference
  0.900000095367431640625
 Valueof Difference
 0.9

我的问题是:valueOf()如何获得精度?在没有手动四舍五入到两位数的情况下,还有其他方法可以得到正确的结果吗?

My question is: What does valueOf() do to get the precision? Is there any other way of getting the correct result without rounding off to the 2 digits manually?

谢谢

推荐答案

查看BigDecimal的源代码,它确实:

Looking at the source code for BigDecimal, it does:

public static BigDecimal valueOf(double val) {
    // Reminder: a zero double returns '0.0', so we cannot fastpath
    // to use the constant ZERO.  This might be important enough to
    // justify a factory approach, a cache, or a few private
    // constants, later.
    return new BigDecimal(Double.toString(val));
}

通过其JavaDoc:

From its JavaDoc:

将double转换为BigDecimal,使用double的规范字符串由...提供的陈述Double.toString(double)方法.

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.

注意:通常,这是首选的方式将double(或float)转换为BigDecimal,因为返回的值是等于从构造一个BigDecimal使用的结果Double.toString(double).

Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).

由于浮点表示,双精度值与您设置的值不完全相同.但是,在String表示期间,它会四舍五入显示的内容.(所有规则都在 JavaDoc ).

Because of floating-point representation, a double value is not exactly what you set it as. However, during String representation, it rounds off what it displays. (All of the rules are on it's JavaDoc).

此外,由于此舍入,如果您这样做:

Furthermore, because of this rounding, if you did:

BigDecimal d = BigDecimal.valueOf(4.00000000000000000000000000000000001));

您会得到错误的值.(d == 4.0)

you would get the wrong value. (d == 4.0)

因此,用字符串初始化这些总是好得多.

So, it's pretty much always better to initialize these with strings.

这篇关于Java BigDecimal差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆