Aaron T.
Aaron T.

Reputation: 1

So i'm having precision issues

So i'm working with java on a currency denominations. 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. 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(). We are also given this....Math.round(amount * 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

 Result:
 3 Quarters
 2 Dimes
 3 Pennies

example 2: 127.97

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

Upvotes: 0

Views: 68

Answers (2)

aparajith vangal
aparajith vangal

Reputation: 34

If the application is dealing with monetary calculations, better to go with 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 money;
System.out.printf("Enter the value: ");
money = new BigDecimal(stdin.nextDouble());

BigDecimal hundreds;

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

Upvotes: 0

kshetline
kshetline

Reputation: 13672

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:

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

Etc., etc., until you get to:

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

pennies = (int) money;

Upvotes: 1

Related Questions