简体   繁体   English

所以我有精度问题

[英]So i'm having precision issues

So i'm working with java on a currency denominations. 因此,我正在使用Java处理货币面额。 the code for the most part is working just fine, but the only issue is with pennies. 该代码大部分都可以正常工作,但唯一的问题是便士。 the problem i'm having is that it will keep leaving out 1 single penny when inputting certain amounts. 我遇到的问题是,输入某些金额时,它会一直遗漏1个一分钱。 Apparently we are suppose to use math.round() to convert a double value to an equivalent long value that represents cents if we want precision....but the biggest problem I have is that I dont even understand what that means let alone even understanding how to implement math.round(). 显然,我们假设使用math.round()将双精度值转换为等效的long值(如果需要精度,则表示美分)。但是,我最大的问题是我什至不了解这意味着什么,更不用说了了解如何实现math.round()。 We are also given this....Math.round(amount * 100). 我们还得到了.... Math.round(金额* 100)。 I'm not expecting any answers, but if anyone can give me any clarity or directions you can point me in. I'd gladly appreciate it, for now i'm really confused and stuck. 我不希望有任何答案,但是如果有人可以给我任何清晰度或指示,您可以向我指出。我很高兴看到它,因为现在我真的很困惑和困惑。

import java.util.Scanner;

public class MoneySorter {

  public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);

    double money;
    System.out.printf("");
    money = stdin.nextDouble(); 

    int hundreds, fifty, twenties, tens, fives, ones;

    int quarters, dimes, nickles, pennies;

    hundreds = (int) money / 100;
    money = money % 100;

    fifty = (int) money / 50; 
    money = money % 50;

    twenties = (int) money / 20; 
    money = money % 20;

    tens = (int) money / 10;
    money = money % 10;

    fives = (int) money / 5;
    money = money % 5;

    ones = (int) money / 1;
    money = money % 1;

    quarters = (int) (money / 0.25);
    money = money % 0.25;

    dimes = (int) (money / 0.10);
    money = money % 0.10;

    nickles = (int) (money / 0.05);
    money = money % 0.05;

    pennies = (int) (money / 0.01);

Some examples would be if I put in the amount: 0.99 例如,如果我输入金额:0.99

 Result:
 3 Quarters
 2 Dimes
 3 Pennies

example 2: 127.97 范例2:127.97

 1 Hundred
 2 Twenties
 1 Five
 2 Ones
 3 Quarters
 2 Dimes
 1 Penny

Don't declare: 不要声明:

double money;

...instead declare: ...而是声明:

long money;

Then replace: 然后替换:

money = stdin.nextDouble();

...with: ...有:

money = Math.round(stdin.nextDouble() * 100);

Realize now that you're dealing with pennies as the main unit of currency, not dollars. 现在意识到您正在将便士作为货币而不是美元的主要单位。 This means you have to multiply much of what follows by 100. For example: 这意味着您必须将其后的大部分乘以100。例如:

hundreds = (int) money / 10000;
money = money % 10000;

Etc., etc., until you get to: 等等,直到您到达:

nickles = (int) (money / 5);
money = money % 5;

pennies = (int) money;

If the application is dealing with monetary calculations, better to go with BigDecimal. 如果应用程序正在处理货币计算,则最好使用BigDecimal。 BigDecimal class is for performing high-precision arithmetic which can be used in banking or financial domain based application https://www.w3resource.com/java-tutorial/java-big-decimal-class.php BigDecimal类用于执行高精度算术,可在基于银行或金融领域的应用程序中使用https://www.w3resource.com/java-tutorial/java-big-decimal-class.php

BigDecimal money;
System.out.printf("Enter the value: ");
money = new BigDecimal(stdin.nextDouble());

BigDecimal hundreds;

hundreds = money.divide(new BigDecimal(100), RoundingMode.CEILING).setScale(2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM