es6 小数保留四位
时间: 2023-11-05 16:21:29 浏览: 80
在ES6中,可以使用toFixed()方法来保留小数位数。例如,如果要保留一个小数的四位,可以使用以下代码:
const num = 0.123456;
const result = num.toFixed(4);
console.log(result); // 输出0.1234
这样就可以将小数保留为四位了。注意,使用toFixed()方法会将结果转换为字符串类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [ES6实用小技巧](https://blog.csdn.net/sinat_34626741/article/details/124187327)[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: 50%"]
- *2* [ES6-Number](https://blog.csdn.net/kelly0721/article/details/89679601)[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: 50%"]
[ .reference_list ]
阅读全文