Method of floating-point precision in Java
First, the content
Generally in Java code to take a double type of floating-point number of precision, rounding or directly out of the way, using the 4 method, recommended the first, I have encapsulated into a tool class.
Second, the Code implementation
① method of using BigDecimal: Roundtool.java (Package as tool class, recommended)
1 Packagecn.com.cxsw.utils;2 3 ImportJava.math.BigDecimal;4 5 /**6 * Some common tool methods related to decimal precision (rounding, etc.).7 * 8 * The accuracy of float/double is divided into the following methods: <br>9 * Java.math.BigDecimal.ROUND_UP <br>Ten * Java.math.BigDecimal.ROUND_DOWN <br> One * Java.math.BigDecimal.ROUND_CEILING <br> A * Java.math.BigDecimal.ROUND_FLOOR <br> - * java.math.bigdecimal.round_half_up<br> - * Java.math.BigDecimal.ROUND_HALF_DOWN <br> the * Java.math.BigDecimal.ROUND_HALF_EVEN <br> - * - * @title Roundtool - * @describe + * @authorZFC - * @date October 25, 2017 morning 11:18:47 + */ A at Public Final classRoundtool { - /** - * Precision of double data is taken. - * <p> - * For example: <br> - * Double value = 100.345678; <br> in * DOUBLE ret = round (value,4,bigdecimal.round_half_up); <br> - * ret for 100.3457 <br> to * + * @paramvalue - * Double data. the * @param Scale * * Precision number of digits (number of decimal digits reserved). $ * @paramRoundingmodePanax Notoginseng * Precision value method. - * @returnthe data after the accuracy calculation. the */ + Public Static DoubleRoundDoubleValueintScaleintRoundingmode) { ABigDecimal BD =NewBigDecimal (value); theBD =Bd.setscale (scale, roundingmode); + DoubleD =Bd.doublevalue (); -BD =NULL; $ returnD; $ } - - /** the * Test using the Main method. - * Wuyi * @paramargc the * Operating parameters. - * Wu */ - Public Static voidMain (string[] argc) { About //The following are examples of preserving 2 decimal places $ - //round_up - //as long as there is a decimal greater than 0 after the 2nd digit, the 2nd bit is +1 -System.out.println (Round (12.3401, 2, bigdecimal.round_up));//12.35 ASystem.out.println (Round ( -12.3401, 2, bigdecimal.round_up));//-12.35 + the //Round_down - //Contrary to round_up $ //Discard all decimals following the 2nd digit directly theSystem.out.println (Round (12.349, 2, Bigdecimal.round_down));//12.34 theSystem.out.println (Round ( -12.349, 2, Bigdecimal.round_down));//-12.34 the the //round_ceiling - //If the number >0 is the same as the ROUND_UP function in //If the number <0 is the same as the Round_down function theSystem.out.println (Round (12.3401, 2, bigdecimal.round_ceiling));//12.35 theSystem.out.println (Round ( -12.349, 2, bigdecimal.round_ceiling));//-12.34 About the //Round_floor the //If the number >0 is the same as the Round_down function the //If the number <0 is the same as the ROUND_UP function +System.out.println (Round (12.349, 2, Bigdecimal.round_floor));//12.34 -System.out.println (Round ( -12.3401, 2, Bigdecimal.round_floor));//-12.35 the Bayi //Round_half_up [This method is most commonly used, rounded] the //if the 3rd digit is >=5, the 2nd digit +1 the //Note: Only the value of the 3rd digit is considered, and the decimal number after the 3rd digit is not taken into account . -System.out.println (Round (12.345, 2, bigdecimal.round_half_up));//12.35 -System.out.println (Round (12.3449, 2, bigdecimal.round_half_up));//12.34 theSystem.out.println (Round ( -12.345, 2, bigdecimal.round_half_up));//-12.35 theSystem.out.println (Round ( -12.3449, 2, bigdecimal.round_half_up));//-12.34 the the //Round_half_down - //if the 3rd digit is >=5, do round_up the //if the 3rd digit is <5, do Round_down theSystem.out.println (Round (12.345, 2, Bigdecimal.round_half_down));//12.35 theSystem.out.println (Round (12.3449, 2, Bigdecimal.round_half_down));//12.3494System.out.println (Round ( -12.345, 2, Bigdecimal.round_half_down));//-12.35 theSystem.out.println (Round ( -12.3449, 2, Bigdecimal.round_half_down));//-12.34 the the //Round_half_even98 //if the 3rd digit is an even number, do Round_half_down About //if the 3rd digit is odd, then do round_half_up -System.out.println (Round (12.346, 2, Bigdecimal.round_half_even));//12.35101System.out.println (Round (12.345, 2, Bigdecimal.round_half_even));//12.35102 103 }104 the}
② Some simple methods: Doublenumberformat.java
1 Packagecn.com.zfc.example;2 3 ImportJava.text.DecimalFormat;4 ImportJava.text.NumberFormat;5 6 /**7 * Number of floating-point numbers of type double (take two decimal places as an example)8 * 9 * @authorZFCTen * One */ A Public classDoublenumberformat { - Public Static voidMain (string[] args) { - Doublenum = 1234.123534; the //1. Use the format () method of String -SYSTEM.OUT.PRINTLN (num + "reserved Two decimal places:" + String.Format ("%.2f"), num)); - - //2. Use the format () method of DecimalFormat +DecimalFormat DecimalFormat =NewDecimalFormat ("#.00"); -SYSTEM.OUT.PRINTLN (num + "reserved Two decimal places:" +Decimalformat.format (num)); + A //3. Use the Format () method of NumberFormat atNumberFormat NumberFormat =numberformat.getnumberinstance (); -Numberformat.setmaximumfractiondigits (2); -SYSTEM.OUT.PRINTLN (num + "reserved Two decimal places:" +Numberformat.format (num)); - } -}
Method of floating-point precision in Java