JS for thousands of separators

Source: Internet
Author: User

A recent project has used thousands to separate this feature, and some examples have been seen on the web, but it is a bit more complex to achieve. Therefore, I realized a thousands of separate, left to us later.

First on the source bar.
The method supports passing in a numeric string, a number. The second parameter is the number of digits reserved for decimals, and two decimal places are reserved by default;

function splitThousands(num, fixed) {    var reg = /\B(?=(\d{3})+$)/g;    num = num.toString().split(".");        num[1] = num[1] ? num[1].substr(0, fixed || 2) : "00000000000000000".substr(0, fixed || 2);        return num.join(".");}

The implementation of this method is very simple, it is a regular problem. In this implementation method, the difficulty is still this regular.
Only the burrow to learn the book, simply look at this regular:
\b with \d, \w and so on, take the opposite direction of \b. \b The word delimiter, it is clear that \b is a non-word delimiter. eg

var str = "hello world, hello MobroZhu, not Mobro Zhu";var reg = [/Mobro\b/g, /Mobro/g];console.log(reg[0], reg[0].exec(str) , "\n" + reg[1], reg[1].exec(str));

Print the log as follows:

/Mobro\b/g ["Mobro", index: 33, input: "hello world, hello MobroZhu, not Mobro Zhu"] "/Mobro/g" ["Mobro", index: 19, input: "hello world, hello MobroZhu, not Mobro Zhu"]

It is obvious that the Mobro found two times is not the same. Without \b, it means that as long as the match to Mobro is counted. So,/mobro/g will match to two Mobro, and/mobro\b/g will only match to the back one Mobro.

He thought, his heart, like this collapsed hole, dim and stuffy, no one is fit to live in;
And his feelings, in people's eyes, just like the mouse, but in the dark and sewage, grow battened, fierce can eat a cat, but still see no light, raves.

Each of the vulgar read Pro "Every jump, see the guy in the shop life cushion"

JS for thousands of separators

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.