JS amount formatted back and forth conversion sample _javascript tips

Source: Internet
Author: User
Tags float number
See the example directly:
Copy Code code as follows:

function Fmoney (s, N)//s: Float number passed in, N: Want to return several decimal points
{
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;
}

The role of this function is to

Call: Fmoney ("12345.675910", 3), return 12,345.676
Copy Code code as follows:

function Rmoney (s)
{
Return parsefloat (S.replace (/[^\d\.-]/g, ""));
}

Returns the number in the format of the amount returned above to the float type.
Copy Code code as follows:

Rmoney (12,345.676)//Return result is: 12345.676

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.