如何在Java中舍入货币值? [英] How do I round up currency values in Java?

查看:111
本文介绍了如何在Java中舍入货币值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的问题。
基本上我有这个问题。

Okay, here's my problem. Basically I have this problem.

我有一个像.53999999这样的数字。
如何在不使用任何数学函数的情况下将其舍入到54?

I have a number like .53999999. How do I round it up to 54 without using any of the Math functions?

我猜我必须乘以100来缩放它,然后划分?
这样的东西?

I'm guessing I have to multiply by 100 to scale it, then divide? Something like that?

问题在于金钱。假设我有50.5399999美元我知道如何获得50美元,但我没有如何获得美分。我可以得到.539999部分,但我不知道怎么把它拿到54美分。

The issue is with money. let's say I have $50.5399999 I know how to get the $50, but I don't have how to get the cents. I can get the .539999 part, but I don't know how to get it to 54 cents.

推荐答案

我会用类似于:

BigDecimal result = new BigDecimal("50.5399999").setScale(2, BigDecimal.ROUND_HALF_UP);

有一篇很棒的文章叫做您应该看一下rel =noreferrer> JavaWorld

There is a great article called Make cents with BigDecimal on JavaWorld that you should take a look at.

这篇关于如何在Java中舍入货币值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆