Skip to content

Instantly share code, notes, and snippets.

@LeonDevLifeLog
Created July 25, 2017 15:23
Show Gist options
  • Save LeonDevLifeLog/536c3bff09803a3df82a269dad1ab6c1 to your computer and use it in GitHub Desktop.
Save LeonDevLifeLog/536c3bff09803a3df82a269dad1ab6c1 to your computer and use it in GitHub Desktop.
java 货币格式化
import Java.text.NumberFormat;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Double myNumber=23323.3323232323;
Double test=0.3434;
//getInstance()
//返回当前缺省语言环境的缺省数值格式。
String myString = NumberFormat.getInstance().format(myNumber);
System.out.println(myString);
//getCurrencyInstance()返回当前缺省语言环境的通用格式
myString = NumberFormat.getCurrencyInstance().format(myNumber);
System.out.println(myString);
//getNumberInstance() 返回当前缺省语言环境的通用数值格式。
myString = NumberFormat.getNumberInstance().format(myNumber);
System.out.println(myString);
//getPercentInstance() 返回当前缺省语言环境的百分比格式。
myString = NumberFormat.getPercentInstance().format(test);
System.out.println(myString);
//setMinimumFractionDigits(int) 设置数值的小数部分允许的最小位数.
//setMaximumFractionDigits(int) 设置数值的小数部分允许的最大位数.
//setMinimumIntegerDigits(int) 设置数值的整数部分允许的最小位数.
//setMaximumIntegerDigits(int) 设置数值的整数部分允许的最大位数.
NumberFormat format = NumberFormat.getInstance();
format.setMinimumFractionDigits( 3 );
format.setMaximumFractionDigits(5);
format.setMaximumIntegerDigits( 10 );
format.setMinimumIntegerDigits(0);
System.out.println(format.format(2132323213.23266666666));
// 结果为:
// 23,323.332
// ¥23,323.33
// 23,323.332
// 34%
// 2,132,323,213.23267
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment