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

327
Vistas
How to compare numbers correctly in JavaScript

I want to compare 2 values based on which I get the color for my legend. But unfortunately I cannot use ceil, floor or round the values as it will effect my result.

I have following color table with first value as point and rest 3 values are r g b values.

Basically I get point from backend which ca be 0.4607441262895224, 0.5500956769649571 and so on. I need to compare the point with first value in color table and give corresponding color. But I am facing issue in comparing , as currently I have given to Fixed(2) but it is not recommended.

"colors": [
[ 0.00, 255, 13, 186 ],
[ 0.25, 254, 4, 135 ],
[ 0.50, 73, 255, 35 ],
[ 0.75, 185, 116, 255 ],
[ 1.00, 32, 50, 255 ]
]

// main logic
const test = arraySet.find((ele) => {
  // point is dynamic
  const point = 0.388920938
  // I cannot use toFixed(2) here, which need to be changed
  // ele[0] is the first value in array 0.00, 0.25, 0.50 and so on
  return point.toFixed(2) == ele[0].toFixed(2);}
);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I suppose you want something on the lines of

Math.abs(point-ele[0]) < 0.01 

replace 0.01 with a threshold relevant to your data, e.g., 1e-6 for 10^(-6)

about 4 years ago · Juan Pablo Isaza Denunciar

0

ToFixed will also be rounded

const toFixed=(num)=>Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try doing it like this

return point.toString().substr(0, ele.toString().length) == ele;
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