admin管理员组

文章数量:1332382

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.

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.

Share edited Jan 5, 2022 at 10:49 sheetaldharerao asked Jan 5, 2022 at 10:37 sheetaldhareraosheetaldharerao 4924 silver badges14 bronze badges 13
  • The nearest 0.05 to 11.10 being 11.05 seems misleading to me, is that intentional? Surely 11.10 is closer to... 11.10 – DBS Commented Jan 5, 2022 at 10:40
  • Yes it is intentional – sheetaldharerao Commented Jan 5, 2022 at 10:41
  • Shouldn't nearest to 11.13 be 11.15 instead of 11.10? – treecon Commented Jan 5, 2022 at 10:41
  • It should be the near to the lower 0.05 value – sheetaldharerao Commented Jan 5, 2022 at 10:42
  • what abput the other five values (x.x6 ....x.x9)? – Nina Scholz Commented Jan 5, 2022 at 10:43
 |  Show 8 more ments

4 Answers 4

Reset to default 7

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; }

You can multiply the values by 100 to temporarily remove the needed two decimal points, the nearest number now bees 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));

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);

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.

本文标签: Check if the value is greater than or equal to the nearest lowest 005 in JavaScriptStack Overflow