如何在Java中将$1319.2295689273974四舍五入为1319.23?

huangapple go评论60阅读模式
英文:

How can I round $1319.2295689273974 to 1319.23 in Java?

问题

以下是您提供的代码的翻译部分:

我正在进行一个月付款的项目,但无法将美元金额保留两位小数。我对编程很新,不知道是否有明显的遗漏。

这是我的代码:

   double monthlyP = (1 + monthlyRate);//P = 付款
   double power = Math.pow(monthlyP, numMonths);
          monthlyP = power * monthlyRate;
   double monthlyP2 = power - 1;
   double FmonthlyP = (monthlyP/monthlyP2) * principle;//F = 最终
          FmonthlyP = (FmonthlyP * 100)/Math.round(100);

它持续输出 1319.0 而不是 0.23。我已经尝试在乘法部分前面和后面放置 Math.round,但无法使其正常工作。

英文:

Im doing a monthly payment project and I cant get the dollar amount to go to two decimals. I'm new to coding and dont know if theres something obvious I'm missing here.

Heres my code:

double monthlyP = (1 + monthlyRate);//P = Payment
double power = Math.pow(monthlyP, numMonths);
       monthlyP = power * monthlyRate;
double monthlyP2 = power - 1;
double FmonthlyP = (monthlyP/monthlyP2) * principle;//F = final
       FmonthlyP = (FmonthlyP * 100)/Math.round(100);

It keeps outputting 1319.0 insted .23. I've tried putting the Math.round in the front of the multiplication part and behind it and I cant get it to work.

答案1

得分: 2

计算机使用64位来表示double类型。64位,可以精确地容纳2^64个不同的值。

噢。

因为有无限多个数字,这是一个大问题。这意味着大多数(实际上,无限多个)数字根本不能被double表示。那么Java怎么办?它会简单地将数字四舍五入到最近可表示的 blessed 数字。

由此产生的结果是,在货币方面使用double是一个大错误

不要这样做。

简单的做法是使用long,来表示你正在处理的货币金额的最小单位。美元和欧元使用分,比特币使用聪。

然后,你的代码可以在'分'(或聪,或其他单位)方面进行所有操作。当你向用户呈现你的金额时,将其传递给一个格式化函数。这样,整个**“如何四舍五入”的问题就无关紧要了**。

格式化函数示例:

public static String printEuro(long cents) {
   boolean negative = cents < 0;
   if (negative) cents = -cents;
   
   int wholes = cents / 100;
   int parts = cents % 100;
   return String.format("%s€%d.%02d", negative ? "-" : "", wholes, parts);
}

注:long类型也是64位的,但double需要能够表示例如1e3000.000123之类的数,而long无法做到。如果我们谈论的是欧元,long可以涵盖每个分数,只要总的欧元金额不超过18位数。即使是整个国家的每个世纪的 GDP 也不会达到这个水平。如果你真的需要走得这么远,可以尝试使用BigInteger

英文:

Computers use 64 bits to represent a double. 64-bits, so that can hold exactly 2^64 different values.

Uhoh.

Because there are an infinite number of numbers, that's a big problem. It means that most (in fact, an infinite amount of them) numbers are not representable by double at all. So what does java do? It will simply silently round things to the nearest of the blessed numbers that it can represent.

The upshot of this is that using double for currency is a big mistake.

Don't do that.

The easy thing to do is to use a long, and represent the atomic unit for the monetary amount you're working with. cents for dollars and euros. satoshi's for bitcoin.

Your code then does all operations in terms of 'cents' (or satoshis or whatnot). When you 'render' your cents to the user, throw it through a formatting function. Now the whole 'how do I round' issue is moot.

Example formatting function:

public static String printEuro(long cents) {
   boolean negative = cents &lt; 0;
   if (negative) cents = -cents;
   
   int wholes = cents / 100;
   int parts = cents % 100;
   return String.format(&quot;%s€%d.%02d&quot;, negative ? &quot;-&quot; : &quot;&quot;, wholes, parts);
}

NB: longs are 64 bit too, but double needs to be able to represent, say, 1e300, or 0.000123, and long doesn't. If we're talking euros, a long can cover every amount of cents as long as the total euro amount has 18 digits or less. Even entire country GDPs per century doesn't get that high. If you really need to go that far, try BigInteger.

答案2

得分: 0

尝试使用:

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

完整文档请查看此处

英文:

Try using

DecimalFormat df = new DecimalFormat(&quot;#.##&quot;);

Full Documentation here

huangapple
  • 本文由 发表于 2020年9月30日 22:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定