Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda