Example of JS amount formatting and back-and-forth conversion

Source: Internet
Author: User
Tags float number

This article mainly introduces the JS amount format conversion realization, the need for friends can refer to the

See the example directly: The code is as follows: function Fmoney (s, N)//s: Float number passed in, N: Want to return a few decimal points {n = n > 0 && n <= 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 function is called: Fmoney ("12345.675910", 3), returns the 12,345.676 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. The code is 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.