简体   繁体   中英

Multiplication with JavaScript

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM