This article mainly introduces how to format a number into an amount in js. You can use the plus or minus commas to process the amount. A code to format the amount. For more information, see.
Example 1: change the number 1111111 to 11,111,111.00 and retain two decimal places.
The Code is 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)
Return '-' + outputdollars (Math. floor (Math. abs (number)-0) + '') + outputcents (Math. abs (number)-0 );
Else
Return outputdollars (Math. floor (number-0) + '') + outputcents (number-0 );
}
// Format the 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>
</Head>
<Body>
<Input type = text maxlength = "8" id = "test" onblur = "this. value = outputmoney (this. value);">
</Body>
</Html>
Online running:
<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)
Return '-' + outputdollars (Math. floor (Math. abs (number)-0) + '') + outputcents (Math. abs (number)-0 );
Else
Return outputdollars (Math. floor (number-0) + '') + outputcents (number-0 );
}
// Format the 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>
</Head>
<Body>
<Input type = text maxlength = "8" id = "test" onblur = "this. value = outputmoney (this. value);">
</Body>
</Html>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Example 2: Convert the number 1111111 to 11,111,111 --- and change 11,111,111 to 1111111 --- integer --
The Code is 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> JavaScript formatting code ---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 = "80" onkeyup = "tran (this)"/>
<Br/>
<Input name = "tt" type = "text" id = "tt2" size = "80" onkeyup = "tran2 (this)"/>
</Body>
</Html>
Run the 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> JavaScript formatting code ---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 = "80" onkeyup = "tran (this)"/>
<Input name = "tt" type = "text" id = "tt2" size = "80" onkeyup = "tran2 (this)"/>
</Body>
</Html>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]