首页 > 解决方案 > Integer always a Floating point number

问题描述

All numbers are changed to floats, with a .0 concatenated to the end.

I have tried parseInt, toFixed, round, floor and every other obvious choice.

A simple example:

Logger.log(list.length);

the log shows float values of type "Number" with a value of 5.0 for the length.

Any attempt to add numbers results in the same

for (i in [1,2,3,4,5]) {
  count++;
  Logger.log(count);
}

The log still shows float values

I would expect Array.length to return an integer

标签: google-apps-script

解决方案


看看这个答案,它解释了数字数据类型在 JavaScript 中的工作原理(Google Apps 脚本基于 JS)。如果您只需要显示小数点前的数字,我建议使用toString()然后substring()将其作为字符串处理,这看起来像这样:

var numStr = count.toString()
var toDisplay = numStr.substr(0,numStr.indexOf("."));
Logger.log(toDisplay);

然后再次将其用作数字,您可以使用Number(toDisplay)此处的文档


推荐阅读