# Question: Converting cents to dollars.

• December 25th, 2009, 10:12 PM
shamed
Question: Converting cents to dollars.
Hi, this is the output of the program I made:

Code :

```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:

Code :

```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?
• December 26th, 2009, 02:52 AM
chronoz13
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
Code :

``` Enter The Peso Value: 49   the total amount is 1 dollar```
• December 26th, 2009, 10:27 AM
copeg
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

Code :

```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"; }```
• December 26th, 2009, 11:48 AM
helloworld922
Re: Question: Converting cents to dollars.
Or, you can use the NumberFormat.getCurencyInstance() method and format your code that way :)

Code :

```double money= 1.234; NumberFormat currency = NumberFormat.getCurrencyInstance(); System.out.println("You have " + currency.format(money));```
• December 26th, 2009, 05:33 PM
shamed
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.

```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!"); } }   }```