简体   繁体   English

如何在谷歌应用程序脚本中将结果设置为小数点后两位

[英]How to set the result to 2 decimal places in google apps script

I have the following code and require the results have 2 decimal places我有以下代码并要求结果有 2 位小数

function TotalVat() {
  const ss = SpreadsheetApp.openById('1BAj2E_xv3bz_Fnt0Xcm5UidGapZhVWeJ6gA5w3-Vnsj');
  const sh = ss.getSheetByName("JOBS");
  const vs = sh.getRange(2, 1, sh.getLastRow() - 2, sh.getLastColumn()).getValues();  
  vs.forEach((r,i) => {
    sh.getRange(i + 2,38).setNumberFormat("0.#0").setValue(r[35] * (r[36]/100));
  });
}

The format on the spreadsheet is 2 decimal places however I am also sending the result by email and here the result from the calculation is more than 2 decimal places.电子表格上的格式是 2 位小数,但我也通过 email 发送结果,这里计算的结果超过 2 位小数。 Any assistance will be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

Try:尝试:

sh.getRange(i + 2,38).setValue(r[35] * (r[36]/100).setNumberFormat("0.00")

This works for me:这对我有用:

function TotalVat() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  vs.forEach((r, i) => {
    sh.getRange(i + 2, 3).setValue(r[0] * (r[1] / 100)).setNumberFormat("0.00");
  });
}
COL1列1 COL2列2
3 3个 10 10 0.30 0.30
9 9 8 8个 0.72 0.72
5 5个 15 15 0.75 0.75
11 11 12 12 1.32 1.32
0 0 13 13 0.00 0.00
0 0 19 19 0.00 0.00
7 7 15 15 1.05 1.05
18 18 17 17 3.06 3.06
18 18 1 1个 0.18 0.18
11 11 17 17 1.87 1.87
9 9 15 15 1.35 1.35
3 3个 3 3个 0.09 0.09
17 17 16 16 2.72 2.72
2 2个 4 4个 0.08 0.08
3 3个 13 13 0.39 0.39
3 3个 8 8个 0.24 0.24
19 19 1 1个 0.19 0.19
19 19 5 5个 0.95 0.95
2 2个 8 8个 0.16 0.16
15 15 10 10 1.50 1.50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM