「JavaScript入門」toFixed()で数値を固定小数点で表示するサンプル

書式
numObj.toFixed([digits])
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function testA(n) {
return Number.parseFloat(n).toFixed(2);
}
console.log("結果1: "+ testA(77.456));
console.log("結果2: "+ testA(1.024));
console.log("結果3: "+ testA('3.23e+5'));
function testA(n) { return Number.parseFloat(n).toFixed(2); } console.log("結果1: "+ testA(77.456)); console.log("結果2: "+ testA(1.024)); console.log("結果3: "+ testA('3.23e+5'));
function testA(n) {
  return Number.parseFloat(n).toFixed(2);
}

console.log("結果1: "+ testA(77.456));
console.log("結果2: "+ testA(1.024));
console.log("結果3: "+ testA('3.23e+5'));

実行結果
> “結果1: 77.46"
> “結果2: 1.02"
> “結果3: 323000.00"

JavaScript

Posted by arkgame