Julie Cohen
Julie Cohen

Reputation: 19

How to write a mathematical formula in Java

I'm trying to figure out a way of converting kilograms (entered by user) to stone and pounds.

For example:

User enters weight as 83.456 kgs, multiply this by 2.204622 to convert to pounds = 184 lbs, divide 184 lbs by 14 to convert to stone = 13.142 stone.

Use the first two digits (13) for stone and separate the remainder to multiply by 14 to get pounds, 0.142 (this is remainder) x 14 = 1.988 lbs, or is there another way to get this result?

Therefore the persons weight is 13 stone and 2 pounds (rounded up or down).

Here's what I have (that works) so far:

pounds = kgs*2.204622;  
System.out.printf("Your weight in pounds is: %.0f" , pounds);
System.out.print(" Ibs\n");
stone = pounds / 14
//Can't figure out how to finish the formula in code

Upvotes: 0

Views: 1844

Answers (3)

Andreas
Andreas

Reputation: 159086

The correct solution rounds early. Here is code as suggested by my initial comment:

double kgs = 83.456;
long pounds = Math.round(kgs*2.204622);
System.out.println("Your weight is " + pounds / 14 + " stone and " + pounds % 14 + " pounds");

Output

Your weight is 13 stone and 2 pounds

If you instead use 69.853 kgs, you get

Your weight is 11 stone and 0 pounds

but this is where thing get dicey if you don't round early.


Both solutions in the (currently accepted) answer by Lightning are wrong, since they round at the wrong time. There is a reason you have to round early.

If you change to use 69.853 kgs in those two solutions, you get

Solution 1:
  Stone: 10
  Pounds: 14

Solution 2:
  Stone: 10
  Pounds: 14.0

Both are obviously incorrect, since Pounds should not be 14, aka 1 stone.

The reason for the rounding errors becomes evident if you print the values without rounding

double kgs = 69.853;
double pounds = kgs*2.204622;
System.out.println(pounds + " lbs = " + pounds / 14 + " stone and " + pounds % 14 + " pounds");

Output

153.99946056599998 lbs = 10.999961468999999 stone and 13.999460565999982 pounds

Upvotes: 0

www.hybriscx.com
www.hybriscx.com

Reputation: 1129

If you are looking for an extensible ready to use library, you can consider Free & open-sourced library UnitOf

It offers 30+ conversion out of the box for Mass.

Example :

double kgFromPound = new UnitOf.Mass().fromPounds(5).toKilograms(); 

double poundFromKg = new UnitOf.Mass().fromKilograms(5).toPounds(); 

Hope it helps!

Upvotes: 1

Lightning
Lightning

Reputation: 405

I'm assuming that you declared pounds and stone before using them here (i.e. with float pounds; or double pounds; or float pounds = something), or the code wouldn't compile otherwise.

One way to do this is to do it in 2 separate steps, as below:

double kg = 83.456;
double pounds = kg * 2.204622;

double stonesWithDecimal = pounds / 14;

int stone = (int) stonesWithDecimal; // Strip off the decimal
long poundsWithoutStone = Math.round((stonesWithDecimal - stone) * 14); // Take the fractional remainder and multiply by 14
System.out.println("Stone: " + stone + "\nPounds: " + poundsWithoutStone);

Andreas's suggestion is definitely much cleaner, though I wanted to present both since I'm not sure what your familiarity level is with using modulo in programming. Here's one implementation of that suggestion, though you can do this a few different ways in terms of dealing with the data types (Math.round wants to return a long):

double kg = 83.456;
double pounds = kg * 2.204622;

int stone = (int) pounds / 14;
pounds = (double) Math.round(pounds %= 14);

System.out.println("Stone: " + stone + "\nPounds: " + pounds);

Upvotes: 2

Related Questions