JavaScript/Number:修订间差异
来自康健生活
| 第12行: | 第12行: | ||
Number {100.345} | Number {100.345} | ||
'100.34' | '100.34' | ||
</pre>}}}} | |||
=== 自定义 === | |||
{{leftrightflex|<syntaxhighlight lang="javascript"> | |||
function fixed(num, digit) { | |||
if(Object.is(parseFloat(num), NaN)) { | |||
return console.log(`传入的值:${num}不是一个数字`); | |||
} | |||
num = parseFloat(num); | |||
return (Math.round((num + Number.EPSILON) * Math.pow(10, digit)) / Math.pow(10, digit)).toFixed(digit); | |||
} | |||
fixed(12.34567, 4);</syntaxhighlight>|{{consoleout|<pre> | |||
'12.3457' | |||
</pre>}}}} | </pre>}}}} | ||
2022年1月19日 (三) 14:19的版本
No categories