如何在 Java 中舍入货币价值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/522855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:47:13  来源:igfitidea点击:

How do I round up currency values in Java?

javacurrencyrounding

提问by

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

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

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

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

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

我猜我必须乘以 100 来缩放它,然后除以?类似的东西?



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.

问题是钱。假设我有 50.5399999 美元,我知道如何获得 50 美元,但我不知道如何获得美分。我可以获得 0.539999 部分,但我不知道如何将其提高到 54 美分。

回答by Chris Ballance

Why would you not want to use any Math functions?

为什么你不想使用任何数学函数?

static long round(double a) 

-Returns the closest long to the argument.

- 返回最接近参数的 long。

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

To represent money I would take the following advice instead of re-inventing the wheel:

为了代表金钱,我会采纳以下建议,而不是重新发明轮子:

http://www.javapractices.com/topic/TopicAction.do?Id=13

http://www.javapractices.com/topic/TopicAction.do?Id=13

回答by Andrew Jaffe

What exactly are you trying to do? Are you always trying to go to two digits? Or are you always trying to fix things like 99999 at the end no matter what?

你到底想做什么?你总是想达到两位数吗?或者你总是试图在最后修复 99999 之类的东西?

According to the comments, it is in fact the former: rounding to dollars and cents. So indeed just round(100*a)/100is what you want. (The latter would be much more complicated...)

根据评论,实际上是前者:四舍五入到美元和美分。所以确实round(100*a)/100正是你想要的。(后者会复杂得多......)

Finally, if you just want to extract the cents part, the following would work:

最后,如果您只想提取美分​​部分,则可以使用以下方法:

dollars_and_cents = round(100*a)/100
cents = (dollars_and_cents-(int)dollars_and_cents)*100

(or does java just have frac? In which case the last line would just be frac(dollars_and_cents)*100.

(或者 java 只有frac?在这种情况下,最后一行就是frac(dollars_and_cents)*100.

回答by recursive

You should use a decimal or currency type to represent money, not floating point.

您应该使用小数或货币类型来表示货币,而不是浮点数。

回答by gnomed

Sean seems to have it, except, if you want to impose proper rules then you may want to throw in an if statement like so:

Sean 似乎有它,除了,如果你想强加适当的规则,那么你可能想要像这样抛出一个 if 语句:

double value = .539999;
int result = (int) (value*100);
if(((value*100)%result)>.5)
    result++;

回答by cgreeno

I would use something like:

我会使用类似的东西:

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

There is a great article called Make cents with BigDecimalon JavaWorldthat you should take a look at.

JavaWorld上有一篇名为Make cents with BigDecimal的好文章,您应该看看。

回答by Jeremy Ruten

You need to make the number .535 and compare that with your original number to see if you'll round up or down. Here's how you get .535 from .53999999 (should work for any number):

您需要将数字设为 0.535 并将其与您的原始数字进行比较,看看您是向上还是向下取整。以下是从 .53999999 获得 .535 的方法(应该适用于任何数字):

num = .53999999;
int_num = (int)(num * 100); // cast to integer, however you do it in Java
compare_num = (int_num + 0.5) / 100;

compare_numwould be .535 in this case. If numis greater than or equal to compare_num, round up to int_num + 1. Otherwise round down simply to int_num.

compare_num在这种情况下将是 0.535。如果num大于或等于compare_num,则向上舍入为int_num + 1。否则简单地向下舍入到int_num

回答by Bill the Lizard

If 50.54 isn't representable in double precision, then rounding won't help.

如果 50.54 不能用双精度表示,那么舍入将无济于事。

If you're trying to convert 50.53999999 to a whole number of dollars and a whole number of cents, do the following:

如果您尝试将 50.53999999 转换为整数美元和整数美分,请执行以下操作:

double d = 50.539999; // or however many 9's, it doesn't matter
int dollars = (int)d;
double frac = d - dollars;
int cents = (int)((frac * 100) + 0.5);

Note that the addition of 0.5 in that last step is to round to the nearestwhole number of cents. If you always want it to round up, change that to add 0.9999999 instead of 0.5.

请注意,在最后一步中添加 0.5 是四舍五入到最接近的整数美分。如果你总是希望它圆,更改,增加0.9999999,而不是0.5。

回答by Peter Lawrey

I suggest you use long for rounding a double value. It won't matter for small numbers but could make a difference.

我建议您使用 long 来舍入双精度值。这对小数字无关紧要,但可能会有所作为。

double d = 50.539999;
long cents = (long)(d * 100 + 0.5);
double rounded = cents/100;

回答by Elijah

Math with money is more complex than most engineers think (over generalization)

数学与金钱比大多数工程师想象的要复杂(过度概括)

If you are doing currency calculations, I think you may be delving into problems that seem simple at their surface but are actually quite complex. For instance, rounding methods that are a result of business logic decisions that are repeated often can drastically affect the totals of calculations.

如果您在进行货币计算,我认为您可能正在深入研究表面上看起来很简单但实际上非常复杂的问题。例如,经常重复的业务逻辑决策导致的舍入方法会极大地影响计算总数。

If this is homework, showing the teacher that you have thought through the real-world problem rather than just slung a bunch of code together that "works" - will surely be more impressive.

如果这是家庭作业,向老师展示您已经考虑过现实世界的问题,而不是将一堆“有效”的代码放在一起 - 肯定会更令人印象深刻。

On a side note, I initially was going to suggest looking at the implementation of the Java math methods in the source code, so I took a look. I noticed that Java was using native methods for its rounding methods - just like it should.

附带说明一下,我最初打算建议查看源代码中 Java 数学方法的实现,所以我看了看。我注意到 Java 在其舍入方法中使用了本机方法 - 就像它应该的那样。

However, a look at BigDecimal shows that there is Java source available for rounding in Java. So rather than just give you the code for your homework, I suggest that you look at the BigDecimal private method doRound(MathContext mc) in the Java source.

但是,查看 BigDecimal 会发现 Java 源代码可用于在 Java 中进行舍入。因此,与其只给你作业的代码,我建议你查看 Java 源代码中的 BigDecimal 私有方法 doRound(MathContext mc)。

回答by basszero

Try storing your currency as number of cents (you could abstract this to number of base curreny units) with a long.

尝试将您的货币存储为美分数(您可以将其抽象为基本货币单位的数量),并带有 long。

Edit: Since this is homework, you may not have control over the types. Consider this a lesson for future projects

编辑:由于这是作业,您可能无法控制类型。将此视为未来项目的一课

long money = 5054;

long cents = money % 100;
long dollars = money / 100; // this works due to integer/long truncation

System.out.printf("$%d.%02.d", dollars, cents);