Javascript converts numeric values to the amount format (separated by kilobytes and automatically added decimal points), and javascript decimal points

Source: Internet
Author: User

Javascript converts numeric values to the amount format (separated by kilobytes and automatically added decimal points), and javascript decimal points

In the project, if a number similar to '123' needs to be converted into the accounting format, '123' is automatically filled when the number of sub-places is separated by two digits after the decimal point is not enough, several implementation methods have been recorded

Ps: if you do not consider the decimal point, the quickest way is:

"12345678". replace (/[0-9] +? (? = (? :( [0-9] {3}) + $)/g, function (a) {return a + ','}); // OUTPUT 12 345 678

1. implement it in a loop

Function formatNum (str) {var newStr = ""; var count = 0; if (str. indexOf (". ") =-1) {for (var I = str. length-1; I> = 0; I --) {if (count % 3 = 0 & count! = 0) {newStr = str. charAt (I) + "," + newStr;} else {newStr = str. charAt (I) + newStr;} count ++;} str = newStr + ". 00 "; // automatically add two consoles after the decimal point. log (str)} else {for (var I = str. indexOf (". ")-1; I> = 0; I --) {if (count % 3 = 0 & count! = 0) {newStr = str. charAt (I) + "," + newStr; // if it is a multiple of 3, add ","} else {newStr = str. charAt (I) + newStr; // concatenate characters one by one} count ++;} str = newStr + (str + "00 "). substr (str + "00 "). indexOf (". "), 3); console. log (str)} formatNum ('100. 13213. 24 '); // output 13,213.34 formatNum ('100. 000. 2 '); // output 132,134.20 formatNum ('20160301'); // output 132134 formatNum ('20160301. 236 '); // output 132,134.236

2. Use Regular Expressions(I still have to judge the number of digits after the decimal point. If you have a more intelligent regular expression, please let me know ~)

Function regexNum (str) {var regex =/(\ d )(? = (\ D) + (?! \ D)/g; if (str. indexOf (". ") =-1) {str = str. replace (regex, ',') + '. 00'; console. log (str)} else {var newStr = str. split ('. '); var str_2 = newStr [0]. replace (regex, ','); if (newStr [1]. length <= 1) {// if there is only one digit after the decimal point, str_2 = str_2 + '. '+ newStr [1] + '0'; console. log (str_2)} else if (newStr [1]. length> 1) {// var decimals = newStr [1] When two or more decimal points exist. substr (0, 2); var srt_3 = str_2 + '. '+ decimals; console. log (srt_3) }}; regexNum ('000000'); // output 23424224, 224.00 regexNum ('2017. 2 '); // output 2, 42, 224.20 regexNum ('2017. 22 '); // output 2, 42, 224.22 regexNum ('2017. 233 '); // output 224.23

The above is all about the content of this article. For more information about the JavaScript syntax, see: JavaScript reference tutorial and JavaScript code style guide. I also hope you can provide more support.

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.