JavaScript小數四捨五入toFixed

來源:互聯網
上載者:User

From: http://kevinpeng.javaeye.com/blog/772591

 

雖然js中Number對象內建了toFixed方法

Java代碼
  1. 2.3567.toFixed(2)  
2.3567.toFixed(2)

但由於使用者使用不同瀏覽器,並且這些瀏覽器js庫也存在些差異,所以表現也不同,大多數時候是在FF下開發,卻忽略了IE等瀏覽器的相容問題。 Java代碼

  1. 原生toFixed方法555.555.toFixed(2) //輸出555.55,IE和FF下執行結果不同  
原生toFixed方法555.555.toFixed(2) //輸出555.55,IE和FF下執行結果不同

借用網上實現代碼: Java代碼

  1. function ForDight(Dight,How){   
  2.   //必須是數字或浮點數。如3.56 、 789   
  3.   //1:先將小數向右移動How位。   
  4.   //2:將移動後結果四捨五入。   
  5.   //3:先將小數向左移動How位。   
  6.   Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);   
  7.   return Dight;   
  8. }   
  9. console.info(ForDight(12345.67890,2));   
function ForDight(Dight,How){  //必須是數字或浮點數。如3.56 、 789  //1:先將小數向右移動How位。  //2:將移動後結果四捨五入。  //3:先將小數向左移動How位。  Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);  return Dight;}console.info(ForDight(12345.67890,2)); 

另外一種實現方法: Java代碼

  1. Number.prototype.toFixed = function(pos){   
  2.   var p = pos || 2; //必須是數字或浮點數,預設精確2位   
  3.   return Math.round(Number(this)*Math.pow(10,p))/Math.pow(10,p);   
  4. }   
  5. console.info((12345.67890).toFixed());  
Number.prototype.toFixed = function(pos){  var p = pos || 2; //必須是數字或浮點數,預設精確2位  return Math.round(Number(this)*Math.pow(10,p))/Math.pow(10,p);}console.info((12345.67890).toFixed());

但還是存在問題

Java代碼
  1. 555.555.toFixed(2)  
555.555.toFixed(2)

輸出結果555.55。

比較好的實現方法: Java代碼

  1. Number.prototype.toFixed=function(len){   
  2.     var add = 0,s,temp;   
  3.     var s1 = this + "";   
  4.     var start = s1.indexOf(".");   
  5.     //必須是數字或浮點數,判斷移動後的前一位是否大於5,大於5加1。   
  6.     if(s1.substr(start+len+1,1)>=5) add=1;   
  7.     var temp = Math.pow(10,len);   
  8.     s = Math.floor(this * temp) + add; // Math.ceil(this * temp)   
  9.     return s/temp;   
  10. }   
  11. 555.555.toFixed(2) //輸出555.56  
Number.prototype.toFixed=function(len){    var add = 0,s,temp;    var s1 = this + "";    var start = s1.indexOf(".");    //必須是數字或浮點數,判斷移動後的前一位是否大於5,大於5加1。    if(s1.substr(start+len+1,1)>=5) add=1;    var temp = Math.pow(10,len);    s = Math.floor(this * temp) + add; // Math.ceil(this * temp)    return s/temp;}555.555.toFixed(2) //輸出555.56

最佳化版: Java代碼

  1. Number.prototype.toFixed=function(len){   
  2.     var temp = Math.pow(10,len);   
  3.     var s = Math.ceil(this * temp)   
  4.     return s/temp;   
  5. }  
Number.prototype.toFixed=function(len){    var temp = Math.pow(10,len);    var s = Math.ceil(this * temp)    return s/temp;}

555.555.toFixed(2) //輸出555.56

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.