JAVA数字格式化
package text; import java.awt.print.Book; import java.lang.reflect.Array; import java.security.Principal; import java.sql.Date; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import javax.xml.crypto.Data; public class text1 { // TODO Auto-generated method stub static public void SimgleFormat(String pattem, double value){ DecimalFormat myFormat = new DecimalFormat(pattem); String output = myFormat.format(value); System.out.println(value + " " + pattem + " " + output); } static public void UseApplypattenMethodFormat(String pattem , double value){ DecimalFormat myFormat = new DecimalFormat(pattem); myFormat.applyPattern(pattem); System.out.println(value + " " + " " + myFormat.format(value)); } public static void main(String[] args) { SimgleFormat("###,###,###.###", 123456.789); SimgleFormat("00000000.####kg", 123456.789); SimgleFormat("000000.000", 123.78); UseApplypattenMethodFormat("#.###%",0.789); UseApplypattenMethodFormat("###.##", 123456.789); UseApplypattenMethodFormat("0.00\u2030", 0.789); } }
1 package text; 2 3 import java.awt.print.Book; 4 import java.lang.reflect.Array; 5 import java.security.Principal; 6 import java.sql.Date; 7 import java.text.DecimalFormat; 8 import java.text.SimpleDateFormat; 9 import java.util.ArrayList; 10 import java.util.Arrays; 11 12 import javax.xml.crypto.Data; 13 14 public class text1 { 15 // TODO Auto-generated method stub 16 public static void main(String[] args) { 17 DecimalFormat myFormat = new DecimalFormat(); 18 myFormat.setGroupingSize(2); 19 String output1 = myFormat.format(123465.789); 20 System.out.println("分组后的结果 "+ output1); 21 myFormat.setGroupingUsed(false); 22 String output2 = myFormat.format(123456.789); 23 System.out.println("未分组的结果 "+ output2); 24 } 25 }