Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!