# a simple conversion problem

• 07-20-2009, 02:50 AM
sweetjava
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)

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; }```
• 07-20-2009, 05:30 AM
angryboy
are you trying to do somethign like:
Code:

`println( Pound +"pounds and "+ ounces +" ounces");`
or maybe I'm misunderstanding your intentions...
• 07-20-2009, 05:44 AM
Mr.Beans
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
Code:

`double ounces = Pound * OUNCES_IN_A_POUND;`
try something like
Code:

`double ounces = ( pound - (int)pound ) * OUNCES_IN_A_POUND;`
Mr. Beans
• 07-20-2009, 06:34 AM
sweetjava
thanks
:)hey thanks Mr.Beans.

That was it exactly!

here is the working code now.

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.