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 5 of 5

Thread: Question: Converting cents to dollars.

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Question: Converting cents to dollars.

    Hi, this is the output of the program I made:

    Enter amount in nickels: 2
    Enter amount in pennies: 100
     
    You entered 2 nickels and 100 pennies.
     
     
    The total amount in penny is 110.0
    The total amount in nickel is 22.0
     
    Converting the amount of nickel and penny into dollar...
     
     
    [B]The total amount in dollar is 1.1[/B]


    How can it be like this:

    Enter amount in nickels: 2
    Enter amount in pennies: 100
     
    You entered 2 nickels and 100 pennies.
     
     
    The total amount in penny is 110.0
    The total amount in nickel is 22.0
     
    Converting the amount of nickel and penny into dollar...
     
     
    [B]The total amount is 1 dollar and 10 cents.[/B]

    What should I do?


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Question: Converting cents to dollars.

    i think you should know how to convert currencies... google is your way to go..

    ill give an example but IDK if this will help your problem


    the currency that we used in our country is peso..

    for each dollar there is (i dont know exactly the value) 49 peso.

    so do the math and some logics...

    so if i enter
     Enter The Peso Value: 49 
     
    the total amount is 1 dollar
    Last edited by chronoz13; December 26th, 2009 at 03:54 AM.

  3. The Following User Says Thank You to chronoz13 For This Useful Post:

    shamed (December 26th, 2009)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question: Converting cents to dollars.

    You can convert your final dollar amount into a string, then parse the string to determine if/where the decimal point is and use that is a guide. For example

    String s = Float.parseFloat(dollarAmount);//assuming dollarAmount is a float containing the value
    int loc = -1;
    String dollars, cents;
    if ( (loc = s.indexOf(".")) != -1 ){
       dollars = s.substring( 0, loc );
       cents = s.substring( loc, s.length() );
        ///get the first two digits from cents here.
    }else{
        dollars = s;
        cents = "0";
    }

  5. The Following 2 Users Say Thank You to copeg For This Useful Post:

    chronoz13 (December 27th, 2009), shamed (December 26th, 2009)

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question: Converting cents to dollars.

    Or, you can use the NumberFormat.getCurencyInstance() method and format your code that way

    double money= 1.234;
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    System.out.println("You have " + currency.format(money));

  7. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 27th, 2009), shamed (December 26th, 2009)

  8. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Question: Converting cents to dollars.

    @ ALL:

    Hi thank you for your reply. Sorry I did not specify that I cannot use advanced coding. I need simple codes for this.

    Someone gave me this advice:
    you get the dollars by taking int(cents / 100)
    you get the cents by taking cents mod 100
    you have to use if statement to check if it's 0 cents (don't show it), 1 cent (use cent instead of cents) or more (use cents)
    But I do not know how to use these.

    This is my program that outputs the "The total amount in dollar is 1.1":
    import java.io.*;
     
    public class CentsDollar {
     
        public static void main (String[]args) {
     
            BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
     
            String x="";
     
            try {
                System.out.println("Enter amount in nickel: ");
                x = input.readLine();
                int nickelin = Integer.parseInt(x);
     
                System.out.println("Enter amount in penny: ");
                x = input.readLine();
                int penniesin = Integer.parseInt(x);
     
                double pennies = (nickelin*5)+(penniesin);
                double nickel = (pennies/5);
     
                System.out.println("You entered " +nickelin+ " nickels and " +penniesin+ " pennies.\n");
                System.out.println("The total amount in penny is "+pennies+".");
                System.out.println("The total amount in nickel is "+nickel+".\n");
                System.out.println("Converting the amount of nickel and penny into dollar...\n\n");
     
                double dollar = (pennies/100);
     
     
                System.out.println("The total amount in dollar is " +dollar+"." );
            }
     
            catch (IOException e) {
                System.out.println("Error!");
            }
        }
     
    }

    Where do I insert the if statement? How do I use the "mod"?

Similar Threads

  1. How to convert GSM 6.10 wav file to MP3/PCM/OGG/AU format using JAVA?
    By expertise in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: April 11th, 2014, 02:13 AM
  2. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  3. cents to dollars and cents program
    By roaster in forum Java Theory & Questions
    Replies: 3
    Last Post: October 24th, 2009, 12:56 AM
  4. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM
  5. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM