JS version of Digital amount format method

Source: Internet
Author: User

/**
* JS version of Digital amount format
* @param string s required to process the number string
* @param string n the number of digits reserved for decimals
*/
function Fmoney (s, N)
{
n = n >= 0 && n <= 20? N:2;
s = parsefloat ((s + ""). Replace (/[^\d\.-]/g, "")). ToFixed (n) + "";//change the number of n here can also determine the number of decimal digits to keep
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? "," : "");
}
if (n>0) {
Return T.split (""). Reverse (). Join ("") + "." + r.substring (0,n);//Leave 2 decimal places if you want to change the last number of substring changes to
}else{
Return T.split (""). Reverse (). Join ("");
}

}

JS version of Digital amount format method

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.