最近做的項目涉及到金額的計算,有一種方式就是進行四舍五入的規則進行小數點后面的尾數處理,以前一直以為toFixed方法就是四舍五入的,知道一個用戶反饋了金額計算的bug我才如夢初醒(虧了一毛錢),才仔細深究了下toFixed這個方法,唉,還是我不夠嚴謹啊,前車之鑒,大家勿走我的老路!
toFixed還不同的瀏覽器實現,在IE10及以上里面是正常的四舍五入,但是別的瀏覽器里面就不一樣了,它不是正常的四舍五入(等下重點說),比如:
var a = 1.335;console.log(a.toFixed(2))// IE 1.34//chorme 1.33
其他的瀏覽器我沒去一一測試,所以如果大家用了其他瀏覽器的還是需要去實際測試一下,我這里就說說javascript的toFixed()方法的四舍五入原理:
toFixed它是一個四舍六入五成雙的詭異的方法(也叫銀行家算法),"四舍六入五成雙"含義:對于位數很多的近似數,當有效位數確定后,其后面多余的數字應該舍去,只保留有效數字最末一位,這種修約(舍入)規則是“四舍六入五成雙”,也即“4舍6入5湊偶”這里“四”是指≤4 時舍去,"六"是指≥6時進上,"五"指的是根據5后面的數字來定,當5后有數時,舍5入1;當5后無有效數字時,需要分兩種情況來講:①5前為奇數,舍5入1;②5前為偶數,舍5不進。(0是偶數)
但是,經過我的測試發現,在chorme下面(最新版),并沒有完全遵守這個規則,尤其是5的后面沒有數字的時候,不是這么判斷的,如下:
var b = 1.335b.toFixed(2)"1.33"var b = 1.345b.toFixed(2)"1.34"var b = 1.355b.toFixed(2)"1.35"var b = 1.365b.toFixed(2)"1.36"var b = 1.375b.toFixed(2)"1.38"var b = 1.385b.toFixed(2)"1.39"
可以發現在chorme下沒有完全去遵循這個規律,或許它有自己的算法,但是畢竟它沒有遵循通用的銀行家算法,所以tofixed這個方法在涉及到金錢計算的業務中還是少用,
最好別用,否則可能會出大問題!
下面再再說說我自己的做法,就是根據精確位數來取小數點后的數,然后判斷精確位是大于4還是小于等于4,上代碼吧,不說了:
我們的業務是最多精確到分,也就是兩位小數,最少就是取整,不留小數
function moneySwitch(money, precision){//precision是需要精確的位數,如百分位就是2 var result = 0; //先進行一個千分位的四舍五入,保證3.0999這種情況在保留一位小數的時候能是對的,這一位可以這么做沒什么問題 var money = parseFloat(money).toFixed(3); try{ var int_part = money.split(".")[0], //小數點前的整數 point_num = money.split(".")[1],//取小數點后面的小數 precision_num = point_num[3-precision]; if(precision_num>4){//五入的情況 if(precision==1){ point_num = parseInt(point_num)+10+""; if(point_num.length>3){//說明往整數位進1 int_part = parseInt(int_part)+1+""; point_num = point_num[1]+point_num[2]; }else{ point_num = point_num[0]+point_num[1]; } result = parseFloat(int_part+"."+point_num); }else if(precision==2){ point_num = parseInt(point_num)+100+""; if(point_num.length>3){//說明往整數位進1 int_part = parseInt(int_part)+1+""; point_num = point_num[1]; }else{ point_num = point_num[0]; } result = parseFloat(int_part+"."+point_num); }else if(precision==3){ int_part = parseInt(int_part)+1+""; point_num = 0; } result = parseFloat(int_part+"."+point_num); }else{//四舍的情況 if(precision==1){ point_num = point_num[0]+point_num[1]; }else if(precision==2){ point_num = point_num[0]; }else if(precision==3){ point_num = 0; } result = parseFloat(int_part+"."+point_num); } }catch(e){ return parseFloat(money).toFixed(2);//如果過程中有出錯就tofixed代替為解決 } return result;}補充:
js處理數字保留2位小數,強制保留2位小數不夠補上.00
1、保留兩位小數 //功能:將浮點數四舍五入,取小數點后2位
2、//制保留2位小數,如:2,會在2后面補上00.即2.00
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Test</title><script type="text/javascript" src="js/jq.js"></script></head><script type="text/javascript"> //保留兩位小數 //功能:將浮點數四舍五入,取小數點后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } //制保留2位小數,如:2,會在2后面補上00.即2.00 function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x*100)/100; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s; } function fomatFloat(src,pos){ return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos); } document.write("四舍五入 <br/>") document.write("3.14159267保留2位小數:" + toDecimal(3.14159267)+"<br/>"); document.write("3.14159267強制保留2位小數:" + toDecimal2(3.14159267)+"<br/>"); document.write("3.14159267保留2位小數:" + toDecimal(3.14559267)+"<br/>"); document.write("3.14159267強制保留2位小數:" + toDecimal2(3.15159267)+"<br/>"); document.write("3.14159267保留2位小數:" + fomatFloat(3.14559267, 2)+"<br/>"); document.write("3.14159267保留1位小數:" + fomatFloat(3.15159267, 1)+"<br/>"); document.write("五舍六入 <br/>") document.write("1000.003保留2位小數:" + 1000.003.toFixed(2)+"<br/>"); document.write("1000.08保留1位小數:" + 1000.08.toFixed(1)+"<br/>"); document.write("1000.04保留1位小數:" + 1000.04.toFixed(1)+"<br/>"); document.write("1000.05保留1位小數:" + 1000.05.toFixed(1)+"<br/>"); document.write("科學計數 <br/>") document.write(3.1415+"科學技術后:"+3.1415.toExponential(2)+"<br/>"); document.write(3.1455+"科學技術后:"+3.1455.toExponential(2)+"<br/>"); document.write(3.1445+"科學技術后:"+3.1445.toExponential(2)+"<br/>"); document.write(3.1465+"科學技術后:"+3.1465.toExponential(2)+"<br/>"); document.write(3.1665+"科學技術后:"+3.1665.toExponential(1)+"<br/>"); document.write("精確到n位,不含n位 <br/>") document.write("3.1415精確到小數點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1455精確到小數點第3位" + 3.1465.toPrecision(3)+"<br/>"); document.write("3.1445精確到小數點第2位" + 3.1415.toPrecision(2)+"<br/>"); document.write("3.1465精確到小數點第2位" + 3.1455.toPrecision(2)+"<br/>"); document.write("3.166592679287精確到小數點第5位" + 3.141592679287.toPrecision(5)+"<br/>"); </script> <body><input type="text" id="Score" /></body></html>這篇關于toFixed()的文章就介紹到這了,希望大家以后多多支持武林網。
新聞熱點
疑難解答