/    Sign up×
Community /Pin to ProfileBookmark

Problem with Number(), parseFloat(), eval()

Does anybody have any idea why this code wouldn’t give me the correct value for the variable “test”:

var test = Number(5) + Number(12.4) + Number(0) + Number(0.4) + Number(0);

I expect the result to be 17.8, instead I get 17.799999999999997 If I use eval() or parseFloat() instead of Number() the result is the same… I don’t want to round up the number.

I’m really curious why the test code above wouldn’t give me the correct result, and what I should do to get the correct result. Thanks!!!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@fredmvMar 02.2004 — [i]Originally posted by eerdem [/i]

[B]var test = [color=blue]([/color]Number(5) + Number(12.4) + Number(0) + Number(0.4) + Number(0)[color=blue]).toFixed(1)[/color];[/B][/QUOTE]
See: http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964.
Copy linkTweet thisAlerts:
@eerdemauthorMar 02.2004 — Thanks fredmv, but toFixed() method is essentially rounding up the number. I don't want to do that. It's weird that for the example I posted I can't get the correct result but for many other numbers it works fine.

A simpler example where again can't get the correct result is this:

var test = Number(5.4) + Number(3.2); alert(test);

Try it... result is NOT 8.6!!!!
Copy linkTweet thisAlerts:
@oleragMar 02.2004 — There was a good example as to the why but I can't find it

as yet - I'll keep looking. But to resolve the problem, you

can issue the toFixed() function, with the desired amount of

decimals.
[code=php]
<script type="text/javascript">
function execute() {
var numb = parseFloat(5+12.4+.4).toFixed(1);
alert("Result: " + numb);
}
</script>
[/code]

fred is too fast for me...

In this [URL=http://forums.webdeveloper.com/showthread.php?s=&threadid=11743&highlight=decimals]thread[/URL] [b] lavalamp[/b] provides a

lengthy solution but I think toFixed() is alot easier. I'm still

looking - there's a thread in here somewhere that provides

an explanation as to why this occurs.

The closest explanation I can find is from [b]pittimann[/b] as to

why JS sometimes provides an erroneous floating point

value and is [URL=http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1207032]here[/URL].
×

Success!

Help @eerdem spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...