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

331
Views
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 answers
Answer question

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 Report

0

ToFixed will also be rounded

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

0

Try doing it like this

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