
1、说明
以使用BigDecimal对货币和百分比进行格式化为例。首先,创建BigDecimal对象,进行BigDecimal的算术操作后,分别建立货币和百分比格式化的引用,最后使用BigDecimal对象作为format()方法的参数,输出其格式化的货币值和百分比。
2、实例
1 2 3 4 5 6 7 8 9 10 11 | NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(3);
BigDecimal loanAmount = new BigDecimal( "15000.48" );
BigDecimal interestRate = new BigDecimal( "0.008" );
BigDecimal interest = loanAmount.multiply(interestRate);
System.out.println( "贷款金额:\t" + currency.format(loanAmount));
System.out.println( "利率:\t" + percent.format(interestRate));
System.out.println( "利息:\t" + currency.format(interest));
|
以上就是java BigDecimal的格式化,希望对大家有所帮助。更多Java学习指路:Java基础
本教程操作环境:windows7系统、java10版,DELL G3电脑。