标签:java
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。
构造方法:
|
1
2
|
BigDecimal volumn = new BigDecimal("0"); volumn = volumn.add(new BigDecimal("1")); |
|
1
2
3
4
5
6
7
8
9
10
|
BigDecimal bigLoanAmount = new BigDecimal("具体数值"); //创建BigDecimal对象BigDecimal bigInterestRate = new BigDecimal("具体数值");BigDecimal bigInterest = bigLoanAmount.multiply(bigInterestRate); //BigDecimal运算NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用NumberFormat percent = NumberFormat.getPercentInstance(); //建立百分比格式化用percent.setMaximumFractionDigits(3); //百分比小数点最多3位//利用BigDecimal对象作为参数在format()中调用货币和百分比格式化System.out.println("Loan amount:\t" + currency.format(bigLoanAmount));System.out.println("Interest rate:\t" + percent.format(bigInterestRate));System.out.println("Interest:\t" + currency.format(bigInterest)); |
|
1
2
3
|
Loan amount: ¥129,876,534,219,876,523.12Interest rate: 8.765%Interest: ¥11,384,239,549,149,661.69 |
标签:java
原文地址:http://blog.csdn.net/wangdianyong/article/details/44156951