js实现四舍五入并保留小数点

2025年02月02日 建站教程

js如何利用tofixed方法和Math.round方法实现四舍五入保留小数点,下面web建站小编给大家详细介绍一下具体实现代码!

利用tofixed方法实现

var nums = 3.1415926;
console.log('保留2位小数点:', nums.toFixed(2)); //保留2位小数点: 3.14
console.log('不留小数点:', nums.toFixed(0)); //不留小数点: 3

利用Math.round方法实现

var nums = 3.1415926;
console.log('输出结果:', Math.round(nums)); //输出结果: 3

本文链接:http://so.lmcjl.com/news/22506/

展开阅读全文
相关内容