在对包含小数点的数据进行截取的时候,可以使用 toFixed() 方法,Number.toFixed(int),其中 Number 是调用者,为 Number 类型的数字,int 为截取小数点后 0 至 n 位。
let tt = 12.1234;
console.log("tt类型",typeof tt,tt);
let cc = tt.toFixed(2);
console.log("cc类型",typeof cc,cc);
通过控制台,我们就可以看到结果了:
tt类型 number 12.1234
cc类型 string 12.12
通过上面的结果,我们可以知道,返回的结果是 12.12,也就是截取了小数点后两位。类型为 string ,那我们在使用他的时候就得注意了,比如调用获取字符串长度、比较大小之类的方法,避免报错