0

My JavaScript (ExtJs 4.1.0) code is:

Ext.getCmp('amount').setValue(Ext.num(Ext.getCmp('unite_price').getValue()) * Ext.num(this.getValue()));

It multiplies 3 with 0.048, and the result is 0.14400000000000002 instead of 0.144.

Why?

ilhan
  • 8,700
  • 35
  • 117
  • 201
  • 3
    possible duplicate of [Elegant workaround for JavaScript floating point number problem](http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem) – zerkms Sep 10 '12 at 08:11

1 Answers1

4

Because of a rounding error in floating point numbers. This is a rather common phenomenon.

If you want 3 decimal points try to round to 3 decimal places.

var result = 3 * 0.048;
var roundedResult = Math.round(result * 1000) / 1000;
Torsten Walter
  • 5,614
  • 23
  • 26