Javascript formatting numeric amounts separated by commas (,) Keep two decimal places _ javascript tips-JS tutorial

Source: Internet
Author: User
JS format the numeric amount with only two decimal places. A Formatting Function is written. The number of decimal places can be controlled, and the number is automatically rounded down. Interested friends can learn about the following:
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:

The 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:

The 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 ):

The 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:
2 3 4 5
+






Appendix:

The 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;
}
}

Related Article

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.