03-固定小数
16.1.1 固定小数
如果需要一个有固定小数位的数字,可以用 Number.prototype.toFixed :
const x = 19.51;
x.toFixed(3); // "19.510"
x.toFixed(2); // "19.51"
x.toFixed(1); // "19.5"
x.toFixed(0); // "20"
注意这里并不是简单粗暴的将数字截断:这个函数的输出是对指定小数位进行四舍五入的结果。