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?

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

Enter The Peso Value: 49
the total amount is 1 dollar

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";
}

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

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:

Quote:

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**":

Code :

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"?