This gadget class is mainly used for numerical rounding, numeric format output, very simple, if you want to further research, please consult the BigDecimal or decimalformat API,BigDecimal. The Rounding method in Setscale (number of digits, rounding method) has the following 7 kinds:
1, Round_up: Rounding away from 0 directions. Rounds in the direction of the absolute value, as long as the discard bit is not 0.
2, Round_down: Trend 0 Direction rounding. Enter in the direction of the smallest absolute value, all the bits are discarded, there is no rounding.
3, Round_ceiling: Rounding to positive infinity. To the positive maximum direction. If positive, the rounding behavior is similar to round_up, and if negative, the rounding behavior is similar to Round_down. The Math.Round () method is the use of this pattern.
4. Round_floor: Rounding in negative infinity direction. To the negative Infinity direction. If positive, the rounding behavior is similar to Round_down, and if negative, the rounding behavior is similar to ROUND_UP.
5, Half_up: The nearest number is rounded (5 in). This is our most classic rounding.
6, Half_down: The most recent number rounding (5 homes). Here 5 is to abandon.
7, Hail_even: Banker rounding Method.
Not much to say, directly on the code, as follows:
1 /**2 * AARON.FFP Inc.3 * Copyright (c) 2004-2016 All rights Reserved.4 */5 PackageCn.ffp.autotest.api.util;6 7 ImportJava.math.BigDecimal;8 ImportJava.math.RoundingMode;9 ImportJava.text.DecimalFormat;Ten One ImportOrg.apache.log4j.Logger; A ImportOrg.apache.log4j.xml.DOMConfigurator; - - ImportCn.ffp.autotest.api.settings.CONSINFO; the - /** - * <strong> calculation tool class </strong><br> - * <br> + * @authorAARON.FFP - * @versionv1.0.0:autotest-api cn.ffp.autotest.api.util Mathutil.java, 2016-04-12 17:51:58.301 Exp $ + */ A Public classMathutil { at Private StaticLogger Logger = Logger.getlogger (mathutil.class. GetName ()); - Private StaticString msg = ""; - - PublicMathutil () { - domconfigurator.configure (Consinfo. Conf_log4j_xml); - } in - /** to * <strong> formatted output for values </strong><br> + * <ul> - * <li> Example: The result of Format ("2.23956", 3) is:2.240</li> the * </ul> * * <br> $ * @authorAARON.FFPPanax Notoginseng * @versionv1.0.0:autotest-api cn.ffp.autotest.api.util mathutil.java format, 2016-04-12 20:13:43.664 EXP $ - * the * @paramdigit to format values + * @paramScale reserved digits (integers less than 1) A * @returnFormatting numeric strings the */ + Public StaticString Format (DoubleDigitintScale ) { -String format = "#."; $ $ if(Scale < 1) { -format = "#"; - } the - for(inti = 0; I < scale; i++) {WuyiFormat + = "0"; the } - Wu returnmathutil.format (digit, format); - } About $ /** - * <strong> formatted output for values </strong><br> - * <ul> - * <li> Formatting example (#.00, reserved 2-bit, #.0000, 4-bit reserved) </li> A * </ul> + * <br> the * @authorAARON.FFP - * @versionv1.0.0:autotest-api cn.ffp.autotest.api.util mathutil.java format, 2016-04-12 19:44:00.926 EXP $ $ * the * @paramdigit to format values the * @paramformat formatting style the * @returnFormatting numeric strings the */ - Private StaticString Format (Doubledigit, String format) { in Try { theDecimalFormat DecimalFormat =Newdecimalformat (format); the About returnDecimalformat.format (digit); the}Catch(NullPointerException npe) { themsg = "The number" "+ digit +" "Based on Style" + format + "Formatting failed, Reason:"; the logger.error (msg, NPE); + - return NULL; the}Catch(illegalargumentexception iae) {Bayimsg = "The number" "+ digit +" "Based on Style" + format + "Formatting failed, Reason:"; the logger.error (msg, IAE); the - return NULL; - } the } the the /** the * <strong> Rounding of values </strong><br> - * <ul> the * <li> Using the Banker rounding method </li> the * </ul> the * <br>94 * @authorAARON.FFP the * @versionV1.0.0:autotest-api cn.ffp.autotest.api.util Mathutil.java scale, 2016-04-12 19:42:52.068 EXP $ the * the * @paramDigit Value98 * @paramScale reserved digits About * @returnValue After rounding - */101 Public StaticString scale (string digit,intScale ) {102 Try {103 if(Scale < 0) {104msg = "" + digit + "" Rounding failed, Reason: Specify the number of digits "" + scale + "" must not be less than 0! Please check! "; the Logger.warn (msg);106 107 return NULL;108 }109 the return NewBigDecimal (digit). Setscale (scale, Roundingmode.half_even). toString ();111}Catch(NumberFormatException nfe) { themsg = "Get" "+ digit +" "Specify Number of digits" "+ scale +" "Rounding failed, Reason:";113 logger.error (msg, NFE); the}Catch(ArithmeticException ae) { themsg = "Get" "+ digit +" "Specify Number of digits" "+ scale +" "Rounding failed, Reason:"; the logger.error (msg, AE);117 }118 119 return NULL; - }121}
Corresponding test Source:
1 PackageCn.ffp.autotest.api.util;2 3 Importorg.testng.annotations.Test;4 5 Public classMathutiltest {6@Test (description = "public static String format (double digit, int. scale)---test")7 Public voidTest_format () {8System.out.println ("Mathutil.format (\" 2.23956\ ", 3) \ t" + Mathutil.format (2.23956, 3));9System.out.println ("Mathutil.format (\" 2.23956\ ", 0) \ t" + mathutil.format (2.23956, 0));TenSystem.out.println ("Mathutil.format (\" 2.23956\ ", -34) \ t" + mathutil.format (2.23956,-34)); One } A -@Test (description = "public static string scale (string digit, int scale)---test") - Public voidTest_scale () { theSystem.out.println ("Mathutil.scale (\" 2.23956\ ", 3) \ t" + mathutil.scale ("2.23956", 3)); -System.out.println ("Mathutil.scale (\" 2.23956\ ", 0) \ t" + mathutil.scale ("2.23956", 0)); -System.out.println ("Mathutil.scale" (\ "2.23956\",-3) \ t "+ mathutil.scale (" 2.23956 ",-3)); - } +}
At this point, Java Learning -047-numeric formatting and the number of decimal rounding successfully completed, I hope this article can give beginners Java you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
Java Learning -047-Numeric formatting and rounding of decimal digits