0

I'm trying to add some values from an array to a cell using Google Script but there are additional , added and the last number is wrong...

Here is my code:

var array = [36776, 37245, 45948, 48877];
ss.getRange(5,11).setValue(array.join());

And the content of that cell is 36,776,372,454,594,800,00 instead of 36776, 37245, 45948, 48877

Valip
  • 4,440
  • 19
  • 79
  • 150

1 Answers1

1

What is happening is due to double-precision floating-point type of number javascript uses.

When you join those numbers and enter in a cell, I guess google spread Sheet is considering this string (which only contains number and comma) a number and converting this string to number. At this point, this is what is happening: Large numbers erroneously rounded in Javascript.

If you want to keep it as you want, add some non numeric value that is not supposed to be in number convention i.e add quotations "36776,37245,45948,48877"

sheet.getRange(5,11).setValue('"' + array.join() + '"');

Or remove the comma entirely to keep the numbers safe from rounding automatically.

sheet.getRange(5,11).setValue(array.join().replace(/,/g, ""));
Community
  • 1
  • 1
m5khan
  • 2,667
  • 1
  • 27
  • 37