[JavaScript]数字取小数点后固定位数

object.toFixed([n])

  1. 舍弃规则为<code>四舍五入</code>
  2. object必须为数字,若为字符串会报异常<code>Uncaught TypeError: test.toFixed is not a function</code>
    <pre>
    var test = "100";
    test.toFixed(1);
    </pre>
  3. 若参数为空,默认位数为0
    <pre>
    var test = "100";
    test = parseInt(test);
    test.toFixed();
    console.log(test);
    </pre>
    结果为:<code>100</code>
  4. 参数范围为[0, 20],若超出范围为会报异常<code>Uncaught RangeError: toFixed() digits argument must be between 0 and 20</code>
    <pre>
    var test = "100";
    test = parseInt(test);
    test.toFixed(21);
    console.log(test);
    </pre>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容