Round a double in Java

前端 未结 6 1058
余生分开走
余生分开走 2020-12-08 00:31

I have found this great solution for rounding:

static Double round(Double d, int precise) {
    BigDecimal bigDecimal = new BigDecimal(d);
    bigDecimal = b         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 00:55

    You may try to change your program like this:-

    static Double round(Double d, int precise) 
    {
    BigDecimal bigDecimal = BigDecimal.valueOf(d);
    bigDecimal = bigDecimal.setScale(precise, RoundingMode.HALF_UP);
    return bigDecimal.doubleValue();
    }
    

    Sample Ideone

    Success  time: 0.07 memory: 381184 signal:0
    Rounded: 2.66
    Rounded: 1.66
    
    Success  time: 0.07 memory: 381248 signal:0
    Rounded: 2.66
    Rounded: 1.66
    

    Reason why you are getting the expected result with BigDecimal.valueOf and not with new BigDecimal, in the words of Joachim Sauer:

    BigDecimal.valueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words: The value of the BigDecimal object will be what you see when you do System.out.println(d).

    If you use new BigDecimal(d) however, then the BigDecimal will try to represent the double value as accurately as possible. This will usually result in a lot more digits being stored than you want.

    Hence resulting in some confusion which you are watching in your program.

    From the Java Doc:

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

    new BigDecimal(double val) -

    Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer. Notes:

    • The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a
      BigDecimal which is exactly equal to 0.1 (an unscaled value of 1,
      with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that
      matter, as a binary fraction of any finite length). Thus, the value
      that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
    • The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
    • When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the
      same result as converting the double to a String using the
      Double.toString(double) method and then using the BigDecimal(String)
      constructor. To get that result, use the static valueOf(double)
      method.

提交回复
热议问题