js 四舍五入保留一位小数怎么写
时间: 2023-09-07 09:16:07 浏览: 157
js四舍五入-保留两位小数
5星 · 资源好评率100%
在JavaScript中,可以使用Math.round()方法来实现四舍五入保留一位小数。下面是一个示例代码:
```javascript
var num = 1.2345;
var roundedNum = Math.round(num * 10) / 10;
console.log(roundedNum); // 输出 1.2
```
在这个例子中,我们将要保留一位小数的数字乘以10,然后使用Math.round()方法对结果进行四舍五入,再除以10,得到保留一位小数的结果。
#### 引用[.reference_title]
- *1* [js四舍五入及任意保留小数位](https://blog.csdn.net/weixin_36346970/article/details/117176222)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [JS实现四舍五入保留两位小数](https://blog.csdn.net/achangjiang123/article/details/126394631)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [JS保留小数方法](https://blog.csdn.net/qq_40938301/article/details/104090772)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文