1 import Java.text.DecimalFormat;2 import Java.text.NumberFormat;3 4 import org.junit.Test;5 6 Public classTestCase {7 /**8 * Replace decimal with percent output9 * The double type is reserved two bits after the decimal point, converted toTen */ One @Test A Public voidTest () { - // ================================================================================ - Doublef =0.5585; the //BigDecimal B = new BigDecimal (f); - //Double f1 = B.setscale (2,bigdecimal.round_half_up). Doublevalue (); - //System.out.println (F1); -System. out. println (Integer.parseint (NewDecimalFormat ("0"). Format (f* -))+"%");//percent no decimal point + //=========================== Preferred =================================================== - Doubleresult1=0.51111122111111; +DecimalFormat DF =NewDecimalFormat ("0.00%"); AString r =Df.format (RESULT1); atSystem. out. println (R);//Great - // ================================================================================= -NumberFormat num =numberformat.getpercentinstance (); -Num.setmaximumintegerdigits (3); -Num.setmaximumfractiondigits (2); - DoubleCSDN =0.55555555555555555; inSystem. out. println (Num.format (CSDN));//Good - // ================================================================================= to Doubleresult=1; + inttemp = (int) (Result * +); -result = (Double) Temp/Ten; theSystem. out. println (Result +"%");//100% became 100.0%. * } $ Panax Notoginseng}
Java converts decimals to percent output