Javascript format the numeric amount separated by commas to keep two decimal places

Source: Internet
Author: User

For example:
12345 format to 12,345.00
12345.6 format to 12,345.60
12345.67 format to 12,345.67
Leave only two decimal places.
After returning, I wrote a Formatting Function. The number of decimal places can be controlled and automatically rounded. The Code is as follows:
Copy codeThe Code is as follows:
Function fmoney (s, n ){
N = n> 0 & n <= 20? N: 2;
S = parseFloat (s + ""). replace (/[^ \ d \.-]/g, ""). toFixed (n) + "";
Var l = s. split (".") [0]. split (""). reverse (), r = s. split (".") [1];
T = "";
For (I = 0; I <l. length; I ++ ){
T + = l [I] + (I + 1) % 3 = 0 & (I + 1 )! = L. length? ",":"");
}
Return t. split (""). reverse (). join ("") + "." + r;
}

Call: fmoney ("12345.675910", 3), return 12,345.676
Restore function:
Copy codeThe Code is as follows:
Function rmoney (s ){
Return parseFloat (s. replace (/[^ \ d \.-]/g ,""));
}

Example (you can save the Code as an html file and run it to view the effect ):
Copy codeThe Code is as follows:
<SCRIPT>
Function fmoney (s, n ){
N = n> 0 & n <= 20? N: 2;
S = parseFloat (s + ""). replace (/[^ \ d \.-]/g, ""). toFixed (n) + "";
Var l = s. split (".") [0]. split (""). reverse (), r = s. split (".") [1];
T = "";
For (I = 0; I <l. length; I ++ ){
T + = l [I] + (I + 1) % 3 = 0 & (I + 1 )! = L. length? ",":"");
}
Return t. split (""). reverse (). join ("") + "." + r;
}
Function rmoney (s ){
Return parseFloat (s. replace (/[^ \ d \.-]/g ,""));
}
Function g (id ){
Return document. getElementById (id );
}
Window. onload = function (){
Var num, txt = g ("txt"), txt2 = g ("txt2"), btn = g ("btn"), btn2 = g ("btn2 "), span = g ("span ");
Btn. onclick = function (){
Num = parseInt (g ("num"). value );
Txt. value = fmoney (txt. value, num );
Txt2.value = fmoney (txt2.value, num );
};
Btn2.onclick = function (){
Num = parseInt (g ("num"). value );
Span. innerHTML = "="
+ Fmoney (rmoney (txt. value) + rmoney (txt2.value), num );
};
};
</SCRIPT>
Number of decimal places:
<Select id = "num">
<Option value = "2"> 2 </option>
<Option value = "3"> 3 </option>
<Option value = "4"> 4 </option>
<Option value = "5"> 5 </option>
</Select>
<Input type = "text" id = "txt" value = "12345.675910"> +
<Input type = "text" id = "txt2" value = "1223"> <span id = "span"> </span>
<Br>
<Input type = "button" id = "btn" value = "format">
<Input type = "button" id = "btn2" value = "add">

Appendix:
Copy codeThe Code is as follows:
/*
* FormatMoney (s, type)
* Function: the amount is separated by commas (,).
* Parameter: s. The value of the amount to be formatted.
* Parameter: type, used to determine whether the formatted amount requires decimal places.
* Return: returns the formatted numeric string.
*/
Function formatMoney (s, type ){
If (/[^ 0-9 \.]/. test (s ))
Return "0 ";
If (s = null | s = "")
Return "0 ";
S = s. toString (). replace (/^ (\ d *) $/, "$1 .");
S = (s + "00"). replace (/(\ d * \. \ d) \ d */, "$1 ");
S = s. replace (".",",");
Var re =/(\ d) (\ d {3 },)/;
While (re. test (s ))
S = s. replace (re, "$1, $2 ");
S = s. replace (/, (\ d) $/, ". $1 ");
If (type = 0) {// no decimal places (decimal places by default)
Var a = s. split (".");
If (a [1] = "00 "){
S = a [0];
}
}
Return s;
}
/*
* Universal DateAdd (interval, number, date) function: implements the date addition function of javascript.
* Parameter: interval, string expression, indicating the time interval to be added. Parameter: number, numeric expression, indicating the number of time intervals to be added. Parameter: date, time object.
* Return: new Time object. var now = new Date (); var newDate = DateAdd ("day", 5, now );
* Author: devinhua (from ○) update:
*/
Function DateAdd (interval, number, date ){
If (date = null)
Return "";
Switch (interval ){
Case "day ":
Date = new Date (date );
Date = date. valueOf ();
Date + = number * 24*60*60*1000;
Date = new Date (date );
Return date;
Break;
Default:
Return "";
Break;
}
}

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.