PackageCom.qiyuan.util;ImportJava.math.BigDecimal;ImportJava.math.RoundingMode;ImportJava.text.DecimalFormat; Public classDecimalutils {/*** (1) to keep the specified number of decimal digits by rounding, the number of digits is not sufficient for 0 supplement (generally not used) *@paramo: decimal number before formatting *@paramnewscale: Number of decimal digits reserved *@returndecimal after formatting*/ /*Public static String Formatdecimalwithzero (Object o, int newscale) {System.out.println ("====================== ====== method 1================================ "); Return String.Format ("%." + Newscale + "F", O); }*/ /*** (2) to keep the specified number of decimal digits by rounding, the number of digits is not sufficient for 0 supplement *@paramd: Decimal number before formatting *@paramnewscale: Number of decimal digits reserved *@returndecimal after formatting*/ Public StaticString Formatdecimalwithzero (DoubleDintNewscale) {System.out.println ("============================ Method 2================================"); String pattern= "0."; for(inti = 0; i < Newscale; i++) {Pattern+ = "0"; } DecimalFormat DF=NewDecimalFormat (pattern); returnDf.format (d); } /*** (3) to keep the specified number of decimal digits by rounding, the number of digits is not sufficient for 0 supplement *@paramd decimal string form before formatting *@paramNewscale the number of decimal digits to keep *@returndecimal after formatting*/ Public StaticString Formatdecimalwithzero (String d,intNewscale) {System.out.println ("============================ Method 3================================"); String pattern= "0."; for(inti = 0; i < Newscale; i++) {Pattern+ = "0"; } DecimalFormat DF=NewDecimalFormat (pattern); returnDf.format (double.valueof (d)); } /*** (4) Keep the specified number of decimal places by rounding, leaving only the number of significant digits after the point *@paramd decimal number before formatting *@paramNewscale the number of decimal digits to keep *@returndecimal after formatting*/ Public StaticString Formatdecimal (DoubleDintNewscale) {System.out.println ("============================ Method 4================================"); String pattern= "#."; for(inti = 0; i < Newscale; i++) {Pattern+= "#"; } DecimalFormat DF=NewDecimalFormat (pattern); returnDf.format (d); } /*** (5) Keep the specified number of decimal places by rounding, leaving only the number of significant digits after the point *@paramd decimal number before formatting *@paramNewscale the number of decimal digits to keep *@returndecimal after formatting*/ Public StaticString Formatdecimal (String d,intNewscale) {System.out.println ("============================ Method 5================================"); String pattern= "#."; for(inti = 0; i < Newscale; i++) {Pattern+= "#"; } DecimalFormat DF=NewDecimalFormat (pattern); returnDf.format (double.valueof (d)); } /*** (6) Keep the specified number of decimal digits in the specified rounding mode *@paramd decimal number before formatting *@paramNewscale the number of decimal digits to keep *@paramRoundingmode Rounding Mode * (Roundingmode.up always go into a/down direct discard/* ceiling positive into negative house/floor negative in/* HALF_UP four Five into/half_down five-loft six-in/* Half_even Banker Rounding Method/unnecessary throws an exception) *@returndecimal after formatting*/ Public Static DoubleFormatdecimal (DoubleDintNewscale, Roundingmode roundingmode) {System.out.println ("============================ Method 6================================"); BigDecimal BD=NewBigDecimal (d). Setscale (Newscale, Roundingmode); returnBd.doublevalue (); } /*** (7) Keep the specified number of decimal digits in the specified rounding mode *@paramd decimal number before formatting *@paramNewscale the number of decimal digits to keep *@paramRoundingmode Rounding Mode * (Roundingmode.up always go into a/down direct discard/* ceiling positive into the negative House/floor negative input/* HALF_UP four Five Into/half_down five-loft/* Half_even Banker Rounding Method/unnecessary throw exception) *@returndecimal after formatting*/ Public Static DoubleFormatdecimal (String D,intNewscale, Roundingmode roundingmode) {System.out.println ("============================ Method 7================================"); BigDecimal BD=NewBigDecimal (double.valueof (d)). Setscale (Newscale, Roundingmode); returnBd.doublevalue (); } Public Static voidMain (string[] args) {System.out.println (The test 2========= retains the specified number of decimal digits by rounding, and the number of digits is not sufficient for the 0 supplement "+decimalutils.formatdecimalwithzero (123.456,5)); System.out.println (The test 2========= retains the specified number of decimal digits by rounding, and the number of digits is not sufficient for the 0 supplement "+decimalutils.formatdecimalwithzero (123.45678901234,5)); System.out.println (The test 3========= retains the specified number of decimal digits by rounding, and the number of digits is not sufficient for 0 supplements "+decimalutils.formatdecimalwithzero (" 123.456 ", 5)); System.out.println (The test 3========= retains the specified number of decimal digits by rounding, and the number of digits is not sufficient for 0 supplements "+decimalutils.formatdecimalwithzero (" 123.45678901234 ", 5)); System.out.println (The test 4========= retains the specified number of decimal places by rounding, leaving only the number of significant digits after the decimal point +decimalutils.formatdecimal (123.456,5)); System.out.println (The test 4========= retains the specified number of decimal places by rounding, leaving only the number of significant digits after the decimal point +decimalutils.formatdecimal (123.45678901234,5)); System.out.println (The test 5========= retains the specified number of decimal places by rounding, leaving only the number of significant digits after the decimal point "+decimalutils.formatdecimal (" 123.456 ", 5)); System.out.println (The test 5========= retains the specified number of decimal places by rounding, leaving only the number of significant digits after the decimal point "+decimalutils.formatdecimal (" 123.45678901234 ", 5)); System.out.println ("Test 6========= keep the specified number of decimal digits in always-on mode" +decimalutils.formatdecimal (123.45678901234,8, roundingmode.up)); System.out.println (The test 6========= retains the specified number of decimal digits by direct truncation mode +decimalutils.formatdecimal (123.45678901234,8, Roundingmode.down)); System.out.println (The test 6========= retains the specified number of decimal digits in the forward negative House mode +decimalutils.formatdecimal (123.45678901234,8, roundingmode.ceiling)); System.out.println ("Test 6========= to keep the specified number of decimal digits in positive-forward negative-loft mode" +decimalutils.formatdecimal ( -123.45678901234,8, roundingmode.ceiling)); System.out.println (The test 6========= retains the specified number of decimal digits in the negative forward mode +decimalutils.formatdecimal (123.45678901234,8, Roundingmode.floor)); System.out.println (The test 6========= retains the specified number of decimal digits in the negative forward mode +decimalutils.formatdecimal ( -123.45678901234,8, Roundingmode.floor)); System.out.println ("Test 6========= keep the specified number of decimal digits in rounding mode" +decimalutils.formatdecimal (123.45678901234,8, roundingmode.half_up)); System.out.println ("Test 6========= keep the specified number of decimal digits in rounding mode" +decimalutils.formatdecimal (123.45678901534,8, roundingmode.half_up)); System.out.println (The test 6========= retains the specified number of decimal digits in five-in-six mode +decimalutils.formatdecimal (123.45678901534,8, Roundingmode.half_down)); System.out.println (The test 6========= retains the specified number of decimal digits in five-in-six mode +decimalutils.formatdecimal (123.45678901634,8, Roundingmode.half_down)); System.out.println (The test 6========= retains the specified number of decimal digits by banker rounding mode +decimalutils.formatdecimal (123.45678901434,8, Roundingmode.half_even)); System.out.println ("Test 7========= to keep the specified number of decimal digits in always-in mode" +decimalutils.formatdecimal ("123.45678901234", 8, roundingmode.up)); System.out.println ("Test 7========= keep the specified number of decimal digits in direct truncation mode" +decimalutils.formatdecimal ("123.45678901234", 8, Roundingmode.down)); System.out.println ("Test 7========= to keep the specified number of decimal digits in the forward negative House mode" +decimalutils.formatdecimal ("123.45678901234", 8, roundingmode.ceiling)); System.out.println ("Test 7========= to keep the specified number of decimal digits in the forward negative House mode" +decimalutils.formatdecimal ("-123.45678901234", 8, roundingmode.ceiling)); System.out.println (The test 7========= retains the specified number of decimal digits in the negative forward mode +decimalutils.formatdecimal ("123.45678901234", 8, Roundingmode.floor)); System.out.println (The test 7========= retains the specified number of decimal digits in the negative forward mode +decimalutils.formatdecimal ("-123.45678901234", 8, Roundingmode.floor)); System.out.println ("Test 7========= keep the specified number of decimal digits in rounding mode" +decimalutils.formatdecimal ("123.45678901234", 8, roundingmode.half_up)); System.out.println ("Test 7========= keep the specified number of decimal digits in rounding mode" +decimalutils.formatdecimal ("123.45678901534", 8, roundingmode.half_up)); System.out.println (The test 7========= retains the specified number of decimal digits in five-in-six mode +decimalutils.formatdecimal ("123.45678901534", 8, Roundingmode.half_down)); System.out.println (The test 7========= retains the specified number of decimal digits in five-in-six mode +decimalutils.formatdecimal ("123.45678901634", 8, Roundingmode.half_down)); System.out.println (The test 7========= retains the specified number of decimal digits by banker rounding mode +decimalutils.formatdecimal ("123.45678901434", 8, Roundingmode.half_even)); }}
Java specifies the method of preserving the number of decimal digits