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

203
Visualizações
JS Rounding 3 decimal points to 2 decimal
Math.round(num * 100) / 100

Case 1:

15.625 * 19 = 296.875

When we apply the code above, the final result is 296.88 which is correct.

var test = 15.625 * 19;
test = Math.round(test * 100) / 100;
console.log( test ); // Return 296.88 (Correct)

Case 2:

15.225 * 19 = 289.275

When we apply the code above, the final result is 289.27 which is not correct.

var test = 15.225 * 19;
test = Math.round(test * 100) / 100;
console.log( test ); // Return 289.27 (Not correct)

Hence, I tried to breakdown and found out that:

289.275 * 100 = 28927.499999999996
Math.round(28927.499999999996) = 28927
Then, 28927/100 = 289.27

The suppose to be 0.5 turned into 0.499999999996 after multiply by 100

(^ This is the part I'm stuck and not sure how to proceed)

For temporary usage, I did something like this as below:

var compiledPrice = 289.275;
console.log( 'compiledPrice1 : ' + compiledPrice );
// 289.275

compiledPrice = compiledPrice * 10;
console.log( 'compiledPrice2 : ' + compiledPrice );
// 2892.75

compiledPrice = compiledPrice.toFixed(1);
console.log( 'compiledPrice3 : ' + compiledPrice );
// 2892.8

compiledPrice = compiledPrice / 10;
console.log( 'compiledPrice4 : ' + compiledPrice );
// 289.28000000000003

compiledPrice = compiledPrice.toFixed(2);
console.log( 'compiledPrice5 : ' + compiledPrice );
// 289.28

I'm not sure is this the proper way to solve this issue, I hope somebody would able to shed some light to me please.

about 4 years ago · Juan Pablo Isaza
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