1

I am using google apps script to implement a function where I need to run ForEach loop over an Array of long integer but it is rounded automatically.

function test(){
 const arr = [1269771000012086445,1269771000011309247];
  arr.forEach(function(a){
     Logger.log(JSON.stringify(a));
  }) 
}

If we run the below code it will give this output :-

1269771000012086500
1269771000011309300

I tried to a few inbuilt math function but no expected output,Is there anything else that I have missed.

URL of the sample sheet - : https://docs.google.com/spreadsheets/d/1q9OPCC1nHV-8ROCzmF8ALMAhFp1ml4BvZLqDLfSxLYY/edit?usp=sharing

Zamir
  • 217
  • 2
  • 11
  • 1
    Related: [this](https://stackoverflow.com/questions/588004) – TheMaster Aug 09 '19 at 11:48
  • 2
    Perhaps the issue might be expressed this way: if you are working with numbers of 16-19 digits then Google Sheets is the wrong tool to use. – Tedinoz Aug 09 '19 at 12:06

2 Answers2

2

The JavaScript Number type is double-precision 64-bit binary format IEEE 754 value. In more recent implementations, JavaScript also supports integers with arbitrary precision using the BigInt type.

Your value exceeds the available memory size so it's stored as rounded. Consider formatting your data in some other way

Дмитро Булах
  • 3,697
  • 1
  • 14
  • 23
  • 2
    your problem couldn't be solved with the existing context since Apps Script doesn't support over-64-bit integer. Consider formatting your data, like storing values as strings `const arr = ['1269771000012086445','1269771000011309247'];` – Дмитро Булах Aug 09 '19 at 11:59
2

These numbers are bigger than

Number.MAX_SAFE_INTEGER
9007199254740991

Maybe use BigInts instead

var x = 1269771000012086445n;
x.toString();
//"1269771000012086445"
  • Did you type the 'n' at the end of the integer? BigInts are not rounded when they are converted to a string. It seems that you need to design your program to use BigInts instead of numbers for the long integers. –  Aug 09 '19 at 11:55
  • @AshnxiousPatch Apps Script doesn't support BigInts – Дмитро Булах Aug 09 '19 at 12:00