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

# Thread: Question: Converting cents to dollars.

1. ## 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?  Reply With Quote

3. ## Re: Question: Converting cents to dollars.

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

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```  Reply With Quote

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

shamed (December 26th, 2009)

5. ## 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";
}```  Reply With Quote

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

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

7. ## 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));```  Reply With Quote

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

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

9. ## 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.

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

String x="";

try {
System.out.println("Enter amount in nickel: ");
int nickelin = Integer.parseInt(x);

System.out.println("Enter amount in penny: ");
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"?  Reply With Quote