有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

双Java双十进制差分

我想看看是否有人能解释为什么下面的代码与valueOf一起工作,而不是其他代码

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()如何获得精度? 有没有其他方法可以在不手动四舍五入到2位数的情况下获得正确的结果

谢谢


共 (4) 个答案

  1. # 1 楼答案

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

    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:

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

    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).

    由于采用浮点表示,双精度值与您设置的值不完全相同。但是,在字符串表示过程中,它会舍入显示的内容。(所有规则都在它的JavaDoc上)

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

    BigDecimal d = BigDecimal.valueOf(4.00000000000000000000000000000000001));

    你会得到错误的值。(d==4.0)

    所以,用字符串初始化这些文件总是比较好的

  2. # 2 楼答案

    BigDecimal.valueOf(double)首先将double转换为String,然后将String转换为BigDecimal

    在第一种情况下,从double或float开始,转换为BigDecimal,计算差值。在第二种情况下,从double或float开始,转换为字符串,然后转换为bigdecim,然后计算差值

  3. # 3 楼答案

    Javadocs开始:

    public static BigDecimal valueOf(double val)

    Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method. 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).

    我想这回答了你的两个问题

    干杯

  4. # 4 楼答案

    因为它调用Double,所以valueOf工作。托斯特林。从Javadoc:

    public static BigDecimal valueOf(double val)
    
        Translates a double into a BigDecimal, using the double's
    

    canonical string representation provided by the Double.toString(double) method.

    当您将一个double传递到bigdecim构造函数中时,构造函数将接受浮点值并精确地复制它。toString代码查找浮点值的近似值

    如果您没有注意到,请使用系统。出来显示浮点数的println()不会显示与先将浮点数包装在BigDecimal中(使用接受双精度的BigDecimal构造函数)相同的结果