long 类里的方法,将一个long类型转换成float
public float floatValue() {
return (float)value;
}
Long paid = 0L;
if(searchResult[14] != null){
paid = Long.valueOf(searchResult[14].toString());
}
BigDecimal bd2 = new BigDecimal(paid.floatValue());
Float f = bd2.divide(divide, 2, BigDecimal.ROUND_HALF_UP).floatValue();
bean.setParkingFee(f);
BigDecimal.ROUND_HALF_UP 往上取:别如 1.23------>1.3
下面是百度的例子
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
4
5
6
7
8
9
10
11
12
13
14
|
Loan amount: ¥
129
,
876
,
534
,
219
,
876
,
523.12
Interest rate:
8.765
%
Interest: ¥
11
,
384
,
239
,
549
,
149
,
661.69
常见用法:
初始化 BigDecimal a=
new
BigDecimal(
"1.35"
);
对数值取值:
1
.a.getScale(
1
,BigDecimal.ROUND_DOWN);
取一位小数,直接删除后面多余位数,故取值
1.3
.
2
.a.getScale(
1
,BigDecimal.ROUND_UP);
取一位小数,删除后面位数,进一位,故取值
1.4
.
3
.a.getScale(
1
,BigDecimal.ROUND_HALF_UP);
取一位小数,四舍五入,故取值
1.4
.
4
.a.getScale(
1
,BigDecimal.ROUND_HALF_DOWN);
取一位小数,四舍五入,但是
5
也是舍弃,故取值
1.3
.
|