Java BigDecimal without E

前端 未结 4 2098
抹茶落季
抹茶落季 2021-01-11 13:38

I have a BigDecimal variable

BigDecimal x = new BigDecimal(\"5521.0000000001\");

Formula:

x = x.add(new BigDecimal(\"-1\")
         


        
4条回答
  •  走了就别回头了
    2021-01-11 13:44

    Perhaps using BigDecimal isn't really helping you.

    double d = 5521.0000000001;
    double f = d - (long) d;
    System.out.printf("%.10f%n", f);
    

    prints

    0.0000000001
    

    but the value 5521.0000000001 is only an approximate representation.

    The actual representation is

    double d = 5521.0000000001;
    System.out.println(new BigDecimal(d));
    BigDecimal db = new BigDecimal(d).subtract(new BigDecimal((long) d));
    System.out.println(db);
    

    prints

    5521.000000000100044417195022106170654296875
    1.00044417195022106170654296875E-10
    

    I suspect whatever you are trying to is not meaningful as you appear to be trying to obtain a value which is not what you think it is.

提交回复
热议问题