Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda