java double截取,在java中截断一个float和一个double

I want to truncate a float and a double value in java.

Following are my requirements:

1. if i have 12.49688f, it should be printed as 12.49 without rounding off

2. if it is 12.456 in double, it should be printed as 12.45 without rounding off

3. In any case if the value is like 12.0, it should be printed as 12 only.

condition 3 is to be always kept in mind.It should be concurrent with truncating logic.

解决方案

try this out-

DecimalFormat df = new DecimalFormat("##.##");

df.setRoundingMode(RoundingMode.DOWN);

System.out.println(df.format(12.49688f));

System.out.println(df.format(12.456));

System.out.println(df.format(12.0));

Here, we are using decimal formatter for formating. The roundmode is set to DOWN, so that it will not auto-round the decimal place.

The expected result is:

12.49

12.45

12

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值