long类型 转换成小数, 利用bigdicimal

 

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 .

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值