Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

471
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda