Skip to content

Instantly share code, notes, and snippets.

@shiranuik
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiranuik/bcde1718694e1f4caead to your computer and use it in GitHub Desktop.
Save shiranuik/bcde1718694e1f4caead to your computer and use it in GitHub Desktop.
javascriptで小数点以下0詰め
// キモはNumber.toFixed()
// これは、Numberを()内で指定した固定小数点の数にしてくれる。
// (0.6).toFixed(3) => 0.600 みたいな感じ。
// formには0詰めされたフォーマットが入っている(0.000みたいな)
// (※何種類かフォーマットを使い分ける必要があったので…
var aaa = 0.6;
var tmp = form.split('\.'); //小数点前後で分割する。
var num = tmp.length==1?0:tmp[1].length; //小数点以下の桁数を取得。小数点がなければ0。
aaa.toFixed(num);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment