Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

472
Views
How to convert feet and inch into cm and back in Javascript
  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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!