介绍
小编给大家分享一下js四舍五入精确到小数点的方法有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
<强>第一种方法:强>
Math.round (num * 100)/100
<强>第二种方法:强>
1。复制下面内容放进代码
function getRound (num位数=2),{ let temp=num; num=Math.round (num * (Math.pow(10(数字+ 1))))/(Math.pow(10(数字+ 1))); let tm  function getRound (num位数=2),{ num=Math.round (num * (Math.pow(10(数字)))); num=num/(Math.pow(10位)); let tempstr=字符串(num); let temp=tempstr.indexOf (“!”); 如果(数字==0){ return 方法(tempstr); }else { 如果(temp>=0) { tempstr.slice (temp,临时+数字); (let 我=temp + 1, i<数字+临时+ 1,我+ +){,& lt;=啊? div=?“在 如果(tempstr.charAt (i)==定义| | tempstr.charAt (i)==null | | tempstr.charAt (i)==啊? { tempstr +=& # 39; 0 & # 39; } } 其他}{ tempstr +=& # 39;强生# 39;; (let i=0; i<数字;我+ +){,& lt;=啊? div=?“在 tempstr +=& # 39; 0 & # 39;; } } return tempstr } }pStr=字符串(num); let tmpArr=tmpStr.split (“!”); 如果(tmpArr.length==1) { 如果(数字==0){ 全国矿工工会return }else { let str=& # 39; & # 39;; (let i=0; i<数字;我+ +){,& lt;=啊? div=?“在 str +=& # 39; 0 & # 39;; } return num + & # 39;强生# 39;+ str; } }else 如果(tmpArr.length==2) { 如果(数字==0){ return Math.round(临时) }else { let tmp=字符串(tmpArr [1]); 如果(tmp.length>数字){ tmp=tmp.slice数字(0,+ 1); 如果(tmp>数字){ tmp=号(tmp)/10; } tmp=方法(tmp); } let str=字符串(tmp); 如果(str.length<数字){,& lt;=啊? div=?“在 (let i=str.length; i<数字;我+ +){,& lt;=啊? div=?“在 str +=& # 39; 0 & # 39;; } } tmp=tmpArr[0] + & # 39;强生# 39;+ str; return tmp; } } }
<强> 2。直接调用此函数:强>
getRound (num位数);//第一个参数为需要四舍五入的数字
第二个参数为精确到小数点几位,默认是2位。
以上是“js四舍五入精确到小数点的方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!