Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2019年2月12日火曜日

小数点以下の桁数を指定したいtoFixed()


123.456789


123.45
のように

小数点第2位まで表示したい




コード.gs
function getFixed() {
  var value = 123.456789;
  var fixed = value.toFixed(2);
  Logger.log(fixed);
}
意訳
この機能がやること
数値
小数点第二位までにして
ログに出す



toFixed()は参考サイトにあるように予期しない結果が発生する可能性もあるらしいので
別の方法も考えてみる

コード.gs
function getFixed2() {
  var value = 123.456789;
  var splited = value.toString().split('.')
  var decimal = splited[1].slice(0, 2);
  var fixed = splited[0] + '.' + decimal
  Logger.log(fixed);
}
意訳
この機能がやること
数値
数値を文字列にして.で区切る
.で区切った後半から2文字目までを取得して
前半と.でつなげて
ログに出す



参考

toFixed
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...