1、说明
以使用BigDecimal对货币和百分比进行格式化为例。首先,创建BigDecimal对象,进行BigDecimal的算术操作后,分别建立货币和百分比格式化的引用,最后使用BigDecimal对象作为format()方法的参数,输出其格式化的货币值和百分比。
2、实例
NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用 NumberFormat percent = NumberFormat.getPercentInstance(); //建立百分比格式化引用 percent.setMaximumFractionDigits(3); //百分比小数点最多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学习指路:
本教程操作环境:windows7系统、java10版,DELL G3电脑。
Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务