Use regular expressions to add commas to every 3 digits

Source: Internet
Author: User

Summing up the method of adding commas to the numbers, consider the general situation. Provide ideas for everyone to refer to the following

#!/usr/bin/perl UseStrict; Usewarnings;my $num=1234556789;#Pure Data Mode$num=~ s/(? <=\d) (? = (\d{3})+$)/,/G;Print $num,"\ n";#with decimal point mode$num=123456.1234;$num=~ s/(? <!\.\d) (? <=\d) (? = (\d{3}) +\b)/,/G;Print $num,"\ n";#after more than 5 bits ... #\k mode, variable-length negation backward view, \k allow the pattern before it to match but not be replaced. The Replace operation is valid only for the string part after \k. $num=123456.12345;$num=~ s/(? <!\.) (?:\ B|\G) \d+?\k (? = (\d{3}) +\b)/,/G;Print $num,"\ n";

OUTPUT: 1,234,556,789 123,456.1234 123,456.12345

Use regular expressions to add commas to every 3 digits

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.