JS Amount Number format implementation code (plus minus comma processing) _javascript tips

Source: Internet
Author: User
Tags abs

Example 1, which turns the number 1111111 to 11,111,111.00 and retains two decimal places.

Copy Code code as follows:

<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)
Return '-' + outputdollars (Math.floor (math.abs (number)-0) + ') + outputcents (math.abs (number)-0);
Else
Return Outputdollars (Math.floor (number-0) + ') + outputcents (number-0);
}
Format 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.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>
<body>
<input type=text maxlength= "8" id= "Test" onblur= "This.value=outputmoney (this.value);" >
</body>


Run Online:

<ptml> <pead> <script type= "Text/javascript" > Function Outputmoney (number) {Number = Number.replac E (/\,/g, ""); if (isNaN (number) | | = Number = "") return ""; Number = Math.Round (number * 100)/100; if (number < 0) return '-' + 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) 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.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> </pead> <body> <input type=text maxlength= "8" id= "Test" onblur= "This.value=outputmone Y (This.value); "> </body> </ptml>
[Ctrl + A All SELECT Note: If the 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--

Copy Code code as follows:

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>js the code for the formatted 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++)
{
RV = (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) = = 44)
{
Rvalue = RVALUE.SUBSTR (1);
}
Id.value = rvalue;
}
function tran2 (ID)
{
var V;
v = id.value.replace (/,/g, "");
Alert (v);
}
</script>
<style type= "Text/css" >
<!--
Body,td,th,input {
font-size:12px;
}
-->
</style><body>
<input name= "tt" type= "text" id= "tt" size= "" onkeyup= "Tran (This)"/>
<br/>
<input name= "tt" type= "text" id= "TT2" size= "" "Onkeyup=" tran2 "(This)"/>
</body>

To run the test:
<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <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++) {rv = (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) = =) {rvalue = Rvalue.substr (1); } id.value = Rvalue; function tran2 (id) {var v; v = id.value.replace (/,/g, ""); Alert (v); } </script> <style type= "Text/css" > <!--body,td,th,input {font-size:12px;}--></style></pead> <body> <input name= "tt" type= "text" id= "tt" size= "no" onkeyup= "Tran" (This) "/> & Lt;input name= "tt" type= "text" id= "TT2" size= "" onkeyup= "tran2 (This)"/> </body> </ptml>
[Ctrl + A All SELECT 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.