0

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
Aaron T.
  • 1
  • 1
  • 3
    Possible duplicate of [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – Jacob G. Feb 26 '18 at 05:58
  • A better approach is to do all calculations with pennies. I.e. multiply the amount by 100, round to an integer and then work with integers only. – Henry Feb 26 '18 at 06:09
  • It doesn't really help me understand with what to do with my code or how to use Math.round or long in this. – Aaron T. Feb 26 '18 at 06:09

2 Answers2

1

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;
kshetline
  • 12,547
  • 4
  • 37
  • 73
  • What is long exactly? I understand that it is bigger than int where int is only 4-bits while long is basically 8-bits. so rounding to a whole number makes everything less messy...the rest i understand the idea of. So the only part i dont really get is the change from double to long. – Aaron T. Feb 26 '18 at 06:22
  • To be precise, and int is 4 bytes, or 32 bits, and a long is 8 bytes, or 64 bits. Now, why use long? First of all, you naturally end up there since the function Math.round() returns a long value from a double -- you'd have to cast back to double to stick with using double, and why do that? From that point on you're dealing with whole numbers. Why long instead of int? A long has all the precision needed to represent every double value that is a precise integer. An int doesn't have enough bits to do that. – kshetline Feb 26 '18 at 06:35
  • I meant bytes, my apologies. Makes a lot of sense, I really appreciate the explanation. I didn't really understand what my professor had meant when he first said "use the Math.round method to convert a double value to an equivalent long value that represents the number of cents." Guess the way it was written out threw me off and made me over think the process. – Aaron T. Feb 26 '18 at 06:44
0

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);