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

179
Visualizações
Round 0.5 to up(1)

can you explain why the number for example 0.3095 is rounded to 0.309 and not to 0.310.

If number is0.3094 rounded is 0.309 that is OK

If number is0.3096 rounded is 0.310 that is OK

The problem is when rounding 5.

I'm using calculations in datatables like:

...
      { "data": "columndata"  ,
      render: function(data, type, row){
          return parseFloat(data/1000).toFixed(3);
      } },
...

Where columndata is 309.500 before calculation.

Here is test case:

https://jsfiddle.net/to8p2hdy/

Test case of working solution:

https://jsfiddle.net/to8p2hdy/1/

Why Math.round do the trick but only parsefloat did not?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You stumbled upon the fact that some decimal numbers do not have an exact representation in the binary system. For example 0.1 can not be represented in the binary system, the closest we get is 0.1000000000000000055511151231257827021181583404541015625.

This is much like "a third" can not be represented in the decimal system because it would need to be 0.333333....

Go to https://www.exploringbinary.com/floating-point-converter/ and find that 0.3095 can not be repesented by a float but instead becomes 0.30949999999999999733546474089962430298328399658203125, which is rounded down instead of up.

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