/** * @Description: Rounding 2 decimal digits * @param valueName * @return */public static Double FormateDouble45 (Double d) { if (d = = null) { return 0d; } BigDecimal B = new BigDecimal (d); Double f = B.setscale (2, bigdecimal.round_half_up). Doublevalue (); return f; } Above is a method that I wrote once to round up the 2 decimal places reserved. Recent discoveries are problematic, such as: 2.555 gets 2.55 instead of the expected 2.56 improvements such as the following: <pre name= "code" class= "java" > /** * @Description: Rounding leaves 2 decimal places * @ Param valueName * @return */public static Double formateDouble45 (Object D) {if (d = = null) {return 0d;} BigDecimal B = new BigDecimal (d.tostring ());d ouble F = B.setscale (2, BIGDECIMAL.ROUND_HALF_UP). Doublevalue (); return f;}
Using a string type when Bugdecimal is constructed by testing the hypothesis. We can achieve all the normal rounding.
I don't know what the reason is.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Java by BigDecimal fillet solution and Precautions