# Thread: a simple conversion problem

1. Member
Join Date
Jul 2009
Posts
7
Rep Power
0

## a simple conversion problem

Hey Guys, im new to java and am working though the Textbook the art and science of JAVA, which is awesome by the way (well so far anyway).

Anyway my problem is I want to write a simple program that converts KG to pounds and ounces.

Converting KG to pounds is simple enough, (kg * 2.2) but my problem is how do i seperate the ounces part?

for example say i had 1KG and converted it to 2.2pounds, how to I then take that .2 remainder and turn that into ounces?

So I can get it to output, 2 pounds and x ounces?

I hope this makes sense, here is my code so far (please don't laugh too hard)

Java Code:
public void run() {

println ("this program converts KG into the old" +
" pounds and ounces ");

double KG_input = readDouble ("enter KG amount:");

double Pound = KG_input * POUNDS_IN_A_KG;

double ounces = Pound * OUNCES_IN_A_POUND;

println (Pound);
}

public static final double POUNDS_IN_A_KG = 2.2 ;
public static final double OUNCES_IN_A_POUND = 16;

}

2. are you trying to do somethign like:
Java Code:
println( Pound +"pounds and "+ ounces +" ounces");
or maybe I'm misunderstanding your intentions...

3. You should take a look at the Java Code Conventions.

The code below that you have gives you the amount of ounces that are in the variable pound. What you want is the remainder
Java Code:
double ounces = Pound * OUNCES_IN_A_POUND;
try something like
Java Code:
double ounces = ( pound - (int)pound ) * OUNCES_IN_A_POUND;
Mr. Beans

4. Member
Join Date
Jul 2009
Posts
7
Rep Power
0

## thanks

:)hey thanks Mr.Beans.

That was it exactly!

here is the working code now.

Java Code:
public void run() {

println ("this program converts KG into the old pounds and ounces ");

double KG_input = readDouble ("enter KG amount:");

double Pound = KG_input * POUNDS_IN_A_KG;
double ounces = ( Pound - (int) Pound ) * OUNCES_IN_A_POUND;

println ((int) (Pound) + "pounds" + (ounces) +"ounces" );

}

public static final double POUNDS_IN_A_KG = 2.2 ;
public static final double OUNCES_IN_A_POUND = 16;

}
thanks again, I think i get it now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•