数字格式化

public static String number_format(double number,String format) {
		return  new DecimalFormat(format).format(number);
	}


运行
System.out.println(number_format(12.48d,"0.0"));

public static String number_format(double number,int decimals,char... separators) {
		DecimalFormat df = new DecimalFormat();
		if(separators!=null){
			DecimalFormatSymbols dfs=df.getDecimalFormatSymbols();
			int length=separators.length;
			switch (length) {
				case 2:
					dfs.setGroupingSeparator(separators[1]);
				case 1:
					dfs.setDecimalSeparator(separators[0]);
			}
			df.setDecimalFormatSymbols(dfs);
		}
		df.setMaximumFractionDigits(decimals);
		r


需要的导入的包
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

你可能感兴趣的:(java)