整数を精度2で浮動小数点に変換したい。-
11 => 11.00
45 => 45.00
私を助けてください。
ありがとうございました。
使用.toFixed
:
var num = 45;
num.toFixed(2); //"45.00"
var num = 10;
var result = num.toFixed(2);
これは、誰かがすでに答えているのを見る非常に一般的な問題です。計算に変換後に数値をさらに使用したい場合は、それに追加したいと思います。コンソールでこれを試してください
a = 10.2222;
typeof a /*"number"*/
a /*10.2222*/
b = parseFloat(b).toFixed(2);
typeof b /*"String"*/
b /*"10.22"*/
c = parseFloat(c)
typeof c /*"number"*/
c /*10.22*/
説明は-
toFixed() method outputs a string variable
but if you want to further use the value of b as a 'number'
you will have to,
c = parseFloat(b)
typeof c
// "number"
toFixed(2)
ここに変数を追加するだけです。