javascript - how to deal with this round off error -


when did simple arithmetic in javascript, there thing kind of round off error. don't want this, don't know how deal it. example:

38.8 * 3 => 116.39999999999999

enter image description here

use this

function multiply (a, b) {    exp = b.tostring().length - 2;    function makeint (num) {        return num * math.pow(10, exp);    }    function makefloat(num) {        return num / math.pow(100, exp);    }    return makefloat(makeint(a) * makeint(b)); }   var result = multiply(38.8,3)  console.log(result); 

https://jsfiddle.net/v51s34st/


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -