Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsbigdecimal

App script spreadsheets float calculation, can google app script do float point number calc?


Can google app script use external library in google app script that can do float number calculation, like bigdecimal?

when I do

var i =  1.15 - 1.12 
console.log(i); 

then i = 0.029999999999999805


Solution

  • Just to answer the question if it's possible, the answer is Yes, based on this SO post.

    However if you wanted to round it up to 2 decimal places only (or more), you don't have to resort to external library, use Math.round:

    function floatNumber(){
    
        var myInt =  1.15 - 1.12 ;
           myInt = Math.round(myInt * 100) / 100;
           Logger.log(myInt);
           //output 0.03
           //if you want 3 decimal places use /1000, and so on.
    }