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

396
Views
Check if the value is greater than or equal to the nearest lowest 0.05 in JavaScript

I want to check whether a decimal value is greater than or equal to the nearest low 0.05 value

Example

Value | Expected Nearest 0.05 value 
11.10 |    11.05           
11.11 |    11.10           
11.12 |    11.10           
11.13 |    11.10           
11.14 |    11.10           
11.15 |    11.10           

I tried using the formula

parseFloat((Math.floor(value * 20) / 20).toFixed(2))

But it fails for 11.10 and 11.15. Using the above formula I get the output same as the value but the expected values are different. Which formula should I use to fix the above test cases.

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

You can multiply the values by 100 to temporarily remove the needed two decimal points, the nearest number now becomes a multiple of 5, you can then remove the rest of the Euclidean division by 5 and get what you want.

And since an exact multiple of 5 needs to be brought to the nearest lower value, you can conditionally remove a 5 when the rest is equal to 0.

The formule function could be something like:

const f = (v) => (((Math.floor(v*100) - (Math.floor(v*100) % 5 || 5)) / 100).toFixed(2));
console.log('11.10', f(11.10));
console.log('11.11', f(11.11));
console.log('11.12', f(11.12));
console.log('11.13', f(11.13));
console.log('11.14', f(11.14));
console.log('11.15', f(11.15));

over 4 years ago · Santiago Trujillo Report

0

You could take an offset and take a multiple floored value.

const format = f => Math.floor((f - 0.01) * 20) / 20;

console.log([11.10, 11.11, 11.12, 11.13, 11.14, 11.15, 11.16, 11.17, 11.18, 11.19].map(format));
.as-console-wrapper { max-height: 100% !important; top: 0; }

over 4 years ago · Santiago Trujillo Report

0

Whether my way is the most efficient I'm not sure, but here's how I would do it:

var x = Math.floor(value * 100);

var remainder = x % 5;

if (remainder == 0) {
   // deal with dropping down the 11.10 to 11.05
   remainder = 5
}

var result = parseFloat((x - remainder) / 100).toFixed(2);

HOWEVER, this only works for values that are 2 decimal places - to account for the third decimal place, you'd need to tweak it to:

var x = value * 100;

var remainder = x % 5;

if (remainder == 0) {
   // deal with dropping down the 11.10 to 11.05
   remainder = 5
}

var result = parseFloat((x - remainder) / 100).toFixed(2);
over 4 years ago · Santiago Trujillo Report

0

What I understand from your question is that you want the nearest lowest of the value and the answer should be less than the value itself. So n being the number you need the lowest multiple of and v being the value, maybe you can try something like:

const nearestMultiple = (v) => (Math.floor((v - 0.001) /n) * n).toFixed(2);

The basic formula for finding a multiple would be Math.floor((v/n) * n) but for your unique case, we don't want the function to return the given number thus the subtracted 0.001.

over 4 years ago · Santiago Trujillo 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!