user2781186
user2781186

Reputation: 45

js decimal precision toFixed() not returning correct value

I am trying to roundOff value with precision 2.

Here is code snippet,

var value = 250.445; var result = (value).toFixed(2);

result contains 250.44, after roundOff it should store 250.45,

I also tried this with directly typing in chrome's console. but still it shows 250.44 value.

Thanks in advance.

Upvotes: 1

Views: 684

Answers (5)

Wiimm
Wiimm

Reputation: 3502

toFixed() works correctly, but most people don't understand the problematic of the limited representation of floating point numbers.

Following the first link: 239.575 is internally stored as 239.574999999999989 and rounding to 2 digits is 239.57 and not 239.58. All user defined replacement functions that have seen work wrong if you enter 239.5749999.

Upvotes: 0

Wiimm
Wiimm

Reputation: 3502

var value = 250.445; var result = (value).toFixed(2); result contains 250.44

250.44 is the absolute correct result, because there exists no exact representation of 250.445 in the computer. If reading 250.445 it is rounded to the nearest possible value = 250.444999999999993 (64 bit float). And rounding this number by toFixed(2) results in 250.44.

btw, 250.444999999999993 is internally stored as 0x406f4e3d70a3d70a and the next higher value is 0x406f4e3d70a3d70b == 250.445000000000022.

Upvotes: 0

Pauloco
Pauloco

Reputation: 1089

The point is, that tofixed() does not always round properly. This has already been ansered here and also here.

The main problem is, that the decimal 250.445 in binary is probably slightly less, leading it to round down the result. There are several self-written functions to address that. E. g.

function toFixed( num, precision ) {
    return (+(Math.round(+(num + 'e' + precision)) + 'e' + -precision)).toFixed(precision);
}

Upvotes: 0

manitoramos
manitoramos

Reputation: 71

You can use:

Math.round(your var or number);

or

Math.floor(your var or number);

$('#teste').html(Math.round(245.655) + " - Round")
$('#testefloor').html(Math.floor(245.655) + " - Floor")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="teste"></label><br>
<label id="testefloor"></label>

Upvotes: 2

Rizkita
Rizkita

Reputation: 98

function round(num,dec)
{
  num = Math.round(num+'e'+dec)
  return Number(num+'e-'+dec)
}

Usage: alert(round(yourNumbber ,decimalsBehindYourNumber));

example: alert(round(2.453,1))

which means round 2.453 into 2.5

have a try!

take a look

Upvotes: 2

Related Questions