JS Amount number Format implementation code

Source: Internet
Author: User
Tags abs split xmlns

  This article mainly introduces the way to format numbers in JS, using plus and minus commas to process the amount, a formatted amount of code, and a friend in need to refer to the following

Example 1, which turns the number 1111111 to 11,111,111.00 and retains two decimal places.   Code as follows: <html> <head> <script type= "Text/javascript" > Function Outputmoney (number) {Number = Number.replace (/,/g, ""); if (isNaN (number) | | = Number = "") return ""; Number = Math.Round (number * 100)/100;     if (number < 0)         '-' + outputdollars (Math.floor (math.abs (number)-0) + ") + outputcents (math.abs (number)-0);     Else         return Outputdollars (Math.floor (number-0) + ") + outputcents (number-0) ; } //Formatted amount function Outputdollars (number) {    if (number.length <= 3)         Retu RN (number = = ""?) ' 0 ': number);     Else {        var mod = number.length% 3;         var output = (mo D = = 0? ': (number.substring (0, MoD));         for (i = 0; i < Math.floor (NUMBER.LENGTH/3); i++) {          &N Bsp if (mod = = 0) && (i = = 0))                 Output + = number.substring (mo D + 3 * I, mod + 3 * i + 3);             else                 Output + = ', ' + num Ber.substring (mod + 3 * I, mod + 3 * i + 3);        }         return (output);    } function outputcents (amount) {    amount = Math.Round (((Amount)-Math.floor (amount)) * 100);     Return (Amount < 10?) '. 0 ' + Amount: '. ' + amount); } </script> </head> <body> <input type=text   maxlength= "8" id= "Test" onblur= "this.value= Outputmoney (This.value); "> </body> </html>     online operation:   <html>  <head>& nbsp <script type= "Text/javascript" >  function Outputmoney (number) {  number = Number.replace (/,/g, "");   if (isNaN (number) | | = Number = "") return "";  number = Math.Round (number *)/100;  if (number < 0)   return '-' + outputdollars (Math.floor math.abs (number )-0) + ') + outputcents (math.abs (number)-0);  else  return Outputdollars (Math.floor (number-0) + ") + out Putcents (number-0); } //Formatted amount   function Outputdollars (number) {  if (number.length <= 3)   return (number = = ""?) ' 0 ': number;  else {  var mod = number.length% 3;  var output = (mod = 0?) ': (number.substring (0, MoD));  for (i = 0; i < Math.floor (NUMBER.LENGTH/3); i++) {  if (mod = 0) ;& (i = = 0))   Output + = number.substring (mod + 3 * I, mod + 3 * i + 3);  else  output = ', ' + NUMBER.S Ubstring (mod + 3 * I, mod + 3 * i + 3); }  return (output); } }  function outputcents (amount) {  amount = Math.Round ((amount)-Math.floor (amount));  return (Amount < 10?) '. 0 ' + Amount: '. ' + amount); }  </script>  </head>  <body>  <input type=text maxlength= "8" id= "Test" onblur= "this.value= Outputmoney (This.value); ">  </body>  </html>     [Ctrl + a full selection Note: If you need to introduce external JS need to refresh to perform]   Example 2, which turns the number 1111111 into 11,111,111---and makes 11,111,111 a 1111111---integer--    code as follows: <!doctype HTML PUBLIC "-//w3c// DTD XHTML 1.0 transitional//en "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD" > <html xmlns= "http:/" /www.w3.org/1999/xhtml "> <head> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>js code that formats the amount---www.jb51.net</title> <script language=" JavaScript "> function Tran (ID) {  var V, J, sj, RV = "";   v = id.value.replace (/,/g, ""). Split (".");   J = v[0].length% 3;   SJ = V[0].SUBSTR (j). ToString ();   for (var i = 0; i < sj.length i++)   {    = (i% 3 = 0)? RV + "," + sj.substr (i, 1): RV + Sj.substr (i, 1);  }   VAR rValue = (v[1] = = undefined)? V[0].substr (0, J) + Rv:v[0].substr (0, J) + RV + "." + v[1];   if (rvalue.charcodeat (0) = n   {    rvalue = RVALUE.SUBSTR (1);  }   Id.value = Rvalue ; function tran2 (ID) {  var v   v = id.value.replace (/,/g, "");   alert (v); </script> <style Ty Pe= "Text/css" > <!--body,td,th,input { font-size:12px;}--> </style></head> <body> & Lt;input name= "tt" type= "text" id= "tt" size= "" onkeyup= "Tran (This)"/> <br/> <input name= "tt" type= "text" I D= "Tt2" size= "onkeyup=" tran2 (This) "/> </body> </html>   Run test: <!doctype HTML PUBLIC"-//w3c// DTD XHTML 1.0 transitional//en "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD" > <html xmlns= "http:/" /www.w3.org/1999/xhtml "> <head> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>js the code that formats the amount---www.jb51.net</title> <script language= "JavaScript" > function tran (ID) {  var V, J, sj, RV = "";   v = id.value.replace (/,/g, ""). Split (".");   J = v[0].length% 3;   SJ = V[0].SUBSTR (j). ToString ();   for (var i = 0; i < sj.length i++)   {    = (i% 3 = 0)? RV + "," + sj.substr (i, 1): RV + Sj.substr (i, 1);  }   var rvalue = (v[1] = = undefined)? V[0].substr (0, J) + Rv:v[0].substr (0, J) + RV + "." + v[1];   if (rvalue.charcodeat (0) = n   {    rvalue = RVALUE.SUBSTR (1);  }   Id.value = Rvalue ; function tran2 (ID) {  var v   v = id.value.replace (/,/g, "");   alert (v); </script> <style Ty Pe= "Text/css" > <!--body,td,th,input { font-size:12px;}--> </style></head> <body> & Lt;input name= "tt" type= "text" id= "tt" size= "" onkeyup= "Tran (This)"/>     <input name= "tt" type= "text" Id= "Tt2" size= "onkeyup=" tran2 (this)/> </body> </html>       [Ctrl + a full election note: If the need to introduce external JS need to refresh to perform]  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.