xxx.toFixed is not a function,js toFixed 报错

 

 

================================

©Copyright 蕃薯耀 2022-03-30

https://www.cnblogs.com/fanshuyao/

 

一、问题描述

count.toFixed(2);

count.toFixed(2);报错:Uncaught TypeError: count.toFixed is not a function

 

二、解决方案


报错的原因是因为count不是Number类型,需要先转成Number

Number(count).toFixed(2);


或者:
parseInt(count).toFixed(2)
parseFloat(count).toFixed(2)

 

 

三、toFixed用法

number.toFixed(x)

把数字转换为字符串,结果的小数点后有指定位数的数字。


x参数:

必需。规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以支持更大的数值范围。如果省略了该参数,将用 0 代替。

 

 

(时间宝贵,分享不易,捐赠回馈,^_^)

 

================================

©Copyright 蕃薯耀 2022-03-30

https://www.cnblogs.com/fanshuyao/

posted @ 2022-03-30 10:32  蕃薯耀  阅读(807)  评论(0)    收藏  举报