Convert kg to pounds and stones in JavaScript
Beginner · 3 min read
Bathroom scales, gym apps and shipping forms often show weight in more than one unit. Kilograms are used in most countries, pounds in the US, and in the UK people often give their body weight in stones and pounds, like "11 stone 3".
In this tutorial you will convert 70 kg to pounds, and then to stones and pounds. The second answer has two parts, so you will return it as an object with two numbers instead of a piece of text. You need to know what a function is, what an object is ({ stones: 11, pounds: 0.3 }), and what Math.floor does (it rounds down to a whole number).
The units
- One pound is exactly 0.45359237 kg. Like the mile, the pound is defined by agreement, so this number is exact.
- One stone is 14 pounds.
To turn kilograms into pounds, divide by 0.45359237. To go back, multiply.
Splitting pounds into stones and pounds
Turning pounds into stones and pounds is like turning minutes into hours and minutes. Divide by 14 and keep the whole number: that is the stones. Whatever is left over is the pounds. JavaScript's % operator gives you what is left over after dividing:
console.log(Math.floor(30 / 14)); // 2
console.log(30 % 14); // 2
30 pounds is 2 stone (28 pounds) with 2 pounds left over.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
70 Kilograms, pounds and stones
easyConvert 70kg to pounds, and to stones and pounds. Return the stones-and-pounds answer as two numbers rather than a string.
Stuck? Three hints, smallest first
- 1. One pound is exactly 0.45359237 kilograms.
- 2. A stone is 14 pounds, so it is a divmod on the pounds figure.
- 3. Return
{ stones, pounds }, and let the display layer decide how to write it.
Step 1: kilograms and pounds
Keep the factor in a named constant and use it both ways:
const KG_PER_LB = 0.45359237; // exact, by definition
const toPounds = (kg) => kg / KG_PER_LB;
const toKg = (lb) => lb * KG_PER_LB;
const round2 = (n) => Number(n.toFixed(2));
console.log(round2(toPounds(70))); // 154.32
console.log(round2(toPounds(50))); // 110.23
console.log(round2(toKg(154))); // 69.85
70 kg is about 154.32 pounds, and a 50 kg bag of rice is about 110.23 pounds. round2 rounds to two decimal places for display; toFixed gives text and Number turns it back into a number.
Step 2: whole stones
Divide the pounds by 14 and round down:
const KG_PER_LB = 0.45359237;
const LB_PER_STONE = 14;
const toPounds = (kg) => kg / KG_PER_LB;
const totalLb = toPounds(70);
console.log(Math.floor(totalLb / LB_PER_STONE)); // 11
154.32 divided by 14 is about 11.02, so 70 kg is 11 whole stones.
Step 3: the pounds left over
Use % for the remainder. On a decimal number, it still works, and gives a decimal remainder:
const KG_PER_LB = 0.45359237;
const totalLb = 70 / KG_PER_LB;
console.log(totalLb % 14); // 0.323583529414293
console.log(Number((totalLb % 14).toFixed(1))); // 0.3
The raw remainder has a long tail of digits, which you don't want in the answer. Rounding it to one decimal place gives 0.3 pounds.
Step 4: return numbers, not text
Put the two parts in an object:
const KG_PER_LB = 0.45359237;
const LB_PER_STONE = 14;
const toPounds = (kg) => kg / KG_PER_LB;
const toKg = (lb) => lb * KG_PER_LB;
function toStonesAndPounds(kg) {
const totalLb = toPounds(kg);
const stones = Math.floor(totalLb / LB_PER_STONE);
return { stones, pounds: Number((totalLb % LB_PER_STONE).toFixed(1)) };
}
console.log(toStonesAndPounds(70)); // { stones: 11, pounds: 0.3 }
console.log(toStonesAndPounds(toKg(14))); // { stones: 1, pounds: 0 }
console.log(toStonesAndPounds(toKg(9))); // { stones: 0, pounds: 9 }
{ stones, pounds: ... } is shorthand: stones on its own means stones: stones.
Why not return '11st 0.3lb'? Text is a dead end. You can't add it, compare it, or show it in a different format without pulling the numbers back out of it. Return numbers from your function, and build the text only where you display it.
The last two lines check the edges. Exactly 14 pounds must be 1 stone and 0 pounds, not 0 stones and 14 pounds; that is where an off-by-one mistake would show. Under 14 pounds gives 0 stones.
The full solution
The answer panel has the conversions both ways and toStonesAndPounds, prints 70 kg in pounds and in stones, a bag of rice, and 154 pounds back in kilograms. Its checks cover exactly one stone, less than a stone, and zero. Press Run to see the output.
const KG_PER_LB = 0.45359237; // exact, by definition
const LB_PER_STONE = 14;
const toPounds = (kg) => kg / KG_PER_LB;
const toKg = (lb) => lb * KG_PER_LB;
function toStonesAndPounds(kg) {
const totalLb = toPounds(kg);
const stones = Math.floor(totalLb / LB_PER_STONE);
return { stones, pounds: Number((totalLb % LB_PER_STONE).toFixed(1)) };
}
const round2 = (n) => Number(n.toFixed(2));
console.log('70 kg ->', round2(toPounds(70)), 'lb');
console.log('70 kg ->', toStonesAndPounds(70));
console.log('a 50kg bag of rice ->', round2(toPounds(50)), 'lb');
console.log('154 lb back to kg ->', round2(toKg(154)));
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('seventy kilograms in pounds', round2(toPounds(70)), 154.32);
test('as stones and pounds', toStonesAndPounds(70), { stones: 11, pounds: 0.3 });
test('a bag of rice', round2(toPounds(50)), 110.23);
test('and back again', round2(toKg(154)), 69.85);
test('one pound of kilograms is one pound', toPounds(KG_PER_LB), 1);
test('exactly fourteen pounds is one stone and none', toStonesAndPounds(toKg(14)), { stones: 1, pounds: 0 });
test('under a stone gives zero stones', toStonesAndPounds(toKg(9)).stones, 0);
test('zero converts to zero', toPounds(0), 0);
Returning { stones, pounds } instead of "11st 0.3lb" is the habit worth taking. A string is a dead end: it cannot be added, compared or re-formatted, and every caller that needs the numbers back has to parse it. Format at the edge, keep numbers inside.
toStonesAndPounds(toKg(14)) is the check that catches an off-by-one — 14lb must be 1 stone and 0 pounds, not 0 stones and 14. Note the toFixed(1) on the remainder: the division is float arithmetic, so the pounds part arrives with a tail of digits that would otherwise show up in the assertion.
Common mistakes
- Multiplying by 0.45359237 to get pounds. That goes the other way. Kilograms to pounds is a division.
- Rounding the stones with
Math.round. 11.6 stones would become 12, and the leftover pounds would no longer fit. UseMath.floor. - Returning a string. Callers then have to pull the numbers back out. Return an object and format it at the end.
Try it yourself
- Convert 85 kg to stones and pounds.
- Write
formatStones({ stones, pounds })that returns text like'11st 0.3lb'. - Write
fromStonesAndPounds(stones, pounds)that returns kilograms, and check that it reversestoStonesAndPounds(70)to within 0.1 kg.