-4

I used javascripts eval function. But i don't like result. How to fix this problem?

Expression:

eval(1.2+24-25)

result:

0.1999999999999993

I want 0.2 result.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Bayanaa
  • 53
  • 9

3 Answers3

10

The problem you have is that 0.2, like "most numbers", has no exact representation in IEEE754 and the problem appears as soon as you combine the number in a computation. You'd have a similar problem with 0.2+1-1.

To get a proper representation, you could reduce to a fixed decimal representation before converting back to a number, for example

var v = +(1.2+24-25).toFixed(10)

This produces

0.2

The general practice is to do all computations with numbers without rounding or using toFixed and only use this kind of transformation when presenting the result to the user (then the + isn't really useful).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    "do all computations with numbers without rounding or...." What if it is an intermediary computation and the next step depends on rounded result? e.g. round off price to nearest tenth and then compute tax. – Abhitalks Jul 08 '13 at 08:18
  • Well, **if** an intermediate step depends on rounded result, then it's part of the computation obviously... – Denys Séguret Jul 08 '13 at 08:24
  • input: `arraytostring = '(( 0.1 + 0.1999999999999993 +( 0 *0.1))-( 0 )-( 0 *0.1))'; var nodeEl = $('input[name=Tag062A]'); nodeEl.val(eval(arraytostring))` This result is 0.29999999999999927. How can i fix this result? Sorry for my English :). – Bayanaa Jul 08 '13 at 09:01
  • 1
    @Bayanaa You may use `nodeEl.val(+eval(arraytostring).toFixed(10))` – Denys Séguret Jul 08 '13 at 09:14
  • 1
    @Bayanaa: I find your choice of accepted answer very surprising. Surely dystroy's answer above is dramatically more useful than the one you selected? It is, of course, entirely up to you what answer you mark as "accepted," I'm just curious why you chose that one rather than this. – T.J. Crowder Jul 08 '13 at 09:20
  • 1
    @Bayanaa Or you might accept Crowder's answer (who answered first). – Denys Séguret Jul 08 '13 at 09:28
  • 1
    @Bayanaa Benjamin, Crowder and Dystroy are right. Their's are more useful and meaningful answers. – Abhitalks Jul 08 '13 at 09:34
  • input: `console.log(eval('1.2+24-25 < 0.2'));` output: true input: `console.log(eval('1.2+24-25 == 0.2'));` output: false i think eval('1.2+24-25')-s result 0.19999... – Bayanaa Jul 10 '13 at 02:58
8

First off, there's no reason at all for eval here. Just:

1.2 + 24 - 25

will do what you want.

The issue here is that the IEEE-754 double-precision floating point numbers used by JavaScript (and other languages) are not perfectly accurate. They're very fast, but not perfectly accurate.

The more famous example is:

0.1 + 0.2

...which gives you 0.30000000000000004.

If you're doing financial calculations or similar where you need the results to be the same as you'd get working with pencil and paper, you'll want a library that does that style of thing (which will be slower, but rather than having the issues like the above, will have the issues you're more familiar with, like not being able to represent 1 / 3 with complete accuracy). This question and its answers here on Stack Overflow discuss various library options.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

use toFixed function

var num =1.2+24-25;
var fixed=num.toFixed(1);

fixed is now "0.2" as a string

Rayee Roded
  • 2,440
  • 1
  • 20
  • 21