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

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

©Copyright 蕃薯耀 2022-03-30

​​蕃薯耀的博客_CSDN博客

一、问题描述

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

​​蕃薯耀的博客_CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.toFixed is not a function的错误出现是因为调用toFixed()方法的对象不是Number类型。解决这个问题的方法是将对象转换为Number类型,可以使用Number()、parseInt()或parseFloat()方法将对象转换为Number类型,然后再调用toFixed()方法来保留指定位数的小数。 另外需要注意的是,toFixed()方法只能用于数字类型,对于字符类型,需要先使用parseFloat()方法将字符转换为Number类型,然后再调用toFixed()方法。所以对于字符类型的要使用parseFloat(value).toFixed(2)来保留2位小数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JS中toFixed()方法引起的问题如何解决](https://download.csdn.net/download/weixin_38609002/13062287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [xxx.toFixed is not a functionjs toFixed 报错](https://blog.csdn.net/w995223851/article/details/123839602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [使用toFixed()函数时,出现“toFixed() is not a function”的解决办法](https://blog.csdn.net/weixin_41997172/article/details/81511639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值