总结一下,在数字中增加逗号的方法,把一般情况都考虑之中.提供思路给大家参考下
#!/usr/bin/perl use strict; use warnings; my $num = 1234556789; #纯数据模式 $num =~ s/(?<=d)(?=(d{3})+$)/,/g; print $num," "; #带有小数点模式 $num = 123456.1234; $num =~ s/(?<!.d)(?<=d)(?=(d{3})+)/,/g; print $num," "; #大于5位之后。。。 #K 模式,可变长否定向后环视;K容许在它之前的模式只匹配而不被替换。替换操作只对K之后的字符串部分有效。 $num = 123456.12345; $num =~ s/(?<!.)(?:|G)d+?K(?=(d{3})+)/,/g; print $num," ";
OUTPUT:
1,234,556,789
123,456.1234
123,456.12345