What can I do against floating point notations Errors?

This question already has an answer here:

  • Why Are Floating Point Numbers Inaccurate? 3 answers
  • Why not use Double or Float to represent currency? 14 answers

  • You can use double for more precision (or BigDecimal if you expect to work with very big numbers).

    String aa = "1000009.00";
    double bb = Double.parseDouble(aa);
    bb=Math.round(bb*100);
    System.out.printf("%.2f", bb); // it prints only two digits after the decimal point
    

    Output

    100000900.00
    

    You can use BigDecimal::multiply for example :

    String aa = "1000009.00";
    aa = aa.replaceAll(",", ".");
    BigDecimal fullValue = new BigDecimal(aa);
    
    System.out.println("full value  = " + fullValue.multiply(new BigDecimal(100)));
    

    Output

    full value  = 100000900.00
    
    链接地址: http://www.djcxy.com/p/85824.html

    上一篇: 如何在Ruby on Rails中将数字强制为10个小数位?

    下一篇: 我能做些什么来对付浮点符号错误?