How to get 2 decimal places in JS and divide the data into thousands of bits

Source: Internet
Author: User

JS in the tofixed (n) method to round the number to the number of the specified number of decimal digits, Note: This method can only be used for data type numbers, including Float,int. For example:

123.12345.toFixed (2);  "123.12" divide the data by using a single statement like this:. Replace (/\d{1,3} (? = (\d{3}) + (\.\d*)? $)/g, ' $&, '). Note: thereplace () method can only work on string types , and our team data is usually divided by the number of decimal divisions, so the data must be converted to a string type before the data is divided into thousands of bits. or use the 123456.1234.toFixed (2) + "" method to convert the data to a string type. For example:123456.1234.toFixed (2). toString (). Replace (/\d{1,3} (? = (\d{3}) + (\.\d*)? $)/g, ' $&, '); "123,456.12"(123456.1234.toFixed (2) + ""). Replace (/\d{1,3} (? = (\d{3}) + (\.\d*)? $)/g, ' $&, '); "123,456.12"Although these 2 methods are very simple, but in the data processing use more, so it is more practical.

How to get 2 decimal places in JS and divide the data into thousands of bits

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.