How to convert feet and inch into cm and back in Javascript

Issue

  1. User picks height in feet. E.g. 6 feet 0 inches
  2. User presses save button. Height is saved into db as cm
  3. User now sees height fetched as cm from db and converted back to feet and inches
  4. Due to Javascript rounding users height is not same as his chosen feet and inches
  5. How to accurately store feet and inches as cm and have the value be same that user picked?

We are using Realm as local database.

const testConversion = (feet: number, inches: number) => {
  const cmTotal = feet * 30.48 + inches * 2.54;

  const feetNew = Math.floor(cmTotal / 30.48);

  // How to have inches match up?
  const inchesNew = (cmTotal % 30.48) * 0.393701;

  expect(feetNew).toEqual(feet);
  expect(inchesNew).toEqual(inches);
};


// 12 inch in a feet
// tallest man about is about 9 feet

describe("Height conversion from metric to imperial", () => {
  it("Amount of stone and lb should stay the same", () => {
    testConversion(6, 0);
    for (let feet = 0; feet < 9; feet++) {
      for (let inches = 0; inches < 12; inches++) {
        testConversion(feet, inches);
      }
    }
  });
});

Result:

‚óŹ Test suite failed to run

expect(received).toEqual(expected) // deep equality

Expected: 0
Received: 12.000006479999998

   8 |
   9 |   expect(feetNew).toEqual(feet);
> 10 |   expect(inchesNew).toEqual(inches);
     |                     ^
  11 | };

Rounding inches in different ways doesn’t seem to help, the end result is always off in some way. Any idea how to do apart from saving feet and inches into separate fields in db?

Solution

Floating-point calculations are never accurate (Is floating point math broken?). And due to that your cmTotal % 30.48 in (cmTotal % 30.48) * 0.393701; is problematic.

You expect that: 182.88 % 30.48 results in 0 but it, however, results in 30.479999999999993 due to the inaccuracies of floating-point arithmetic.

What you want to do is, that the result of newInch depends on the result you calculated for feetNew:

(cmTotal - feetNew * 30.48) * 0.393701;

And in your test you shouldn’t do expect(inchesNew).toEqual(inches); because you can’t expect that the numbers of such conversion are exactly equal. You will always have some small error. So you probably want to use something like toBeCloseTo instead.

let feet = 6
let inches = 0

const testConversion = (feet, inches) => {
  const cmTotal = feet * 30.48 + inches * 2.54;

  const feetNew = Math.floor(cmTotal / 30.48);

  // How to have inches match up?
  const inchesNew = (cmTotal - feetNew * 30.48) * 0.393701;

  console.log(feet, feetNew)
  console.log(inches, inchesNew)
};


testConversion(6, 0)

Answered By – t.niese

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published