Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why is it not ROUNDING HELP PLEASE

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Why is it not ROUNDING HELP PLEASE

    Okay so I am supposed to write a code which accepts a certain amount of money ( 7.53) which then outputs what coins i need to make this amount up, i try it and compile it, but its giving me the interactions shown below as decimals and not whole numbers. (obvs it doesnt look good to have 0.003765 toonies) Please HELP THANKS



    import java.util.*;
    import javax.swing.*;
    public class CoinCounter {

    public static void main (String [] args){

    Scanner sc = new Scanner(System.in);
    String userInput;
    double amount;
    int loonie;
    int toonie;
    int quarter;
    int dime;
    int nickle;
    int pennie;
    int totalCoins = 0;
    double myCoins;

    userInput = JOptionPane.showInputDialog("Please enter an amount");
    amount = Double.parseDouble(userInput);
    System.out.println("Enter the amount");
    amount=sc.nextDouble();


    System.out.println((amount / 200) + " – toonies");
    myCoins = (amount / 200);
    amount = amount % 200;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }

    System.out.println((amount / 100) + " – loonies");
    myCoins = (amount / 100);
    amount = amount % 100;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }

    System.out.println((amount / 25) + " – quarters");
    myCoins = (amount / 25);
    amount = amount % 25;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }

    System.out.println((amount / 10) + " – dimes");
    myCoins = (amount / 10);
    amount = amount % 10;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }

    System.out.println((amount / 5) + " –Nickles");
    myCoins = (amount / 5);
    amount = amount % 5;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }


    System.out.println((amount/1) + " – pennies");
    myCoins = (amount);
    amount = amount % ;

    if(amount > 0){
    for(int a = 0; a < myCoins; a++ ){
    totalCoins++;
    }
    }

    System.out.println("Total Coins: " + totalCoins);

    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why is it not ROUNDING HELP PLEASE

    One way to handle money is to use whole amounts instead of fractional amounts. By that I mean use 753 instead of 7.53. Hopefully you know what the problem is. If not Google "What every computer scientist should know about float point numbers" or something like that.

    --- Update ---

    for(int a = 0; a < myCoins; a++ ){
        totalCoins++;
    }
    I thought the objective was to work out how many of each coin denomination there is. But you never use the loonie, toonie etc variables. Also why are you reading in the amount twice? Once via a dialog and once via a Scanner.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why is it not ROUNDING HELP PLEASE

    Please read the Announcement at the top of the sub-Forums to learn how to post your code in code/formatting tags and how to ask questions that get attention and receive helpful answers.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    quantamphysics (October 2nd, 2013)

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Why is it not ROUNDING HELP PLEASE

    What is your conversion rate between all the coins? If you can post that I might be able to help.

  6. The Following User Says Thank You to Totara For This Useful Post:

    quantamphysics (October 2nd, 2013)

Similar Threads

  1. Rounding with printf
    By Brandos12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2013, 07:22 PM
  2. Rounding Question
    By FrozenFox in forum Algorithms & Recursion
    Replies: 1
    Last Post: July 23rd, 2012, 02:29 PM
  3. [SOLVED] Double rounding
    By krillov in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 12th, 2012, 01:09 PM
  4. Question about rounding
    By CoolGuy134 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 27th, 2011, 04:30 PM
  5. Rounding screwed up?
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2010, 01:43 PM