perl数字格式转换
查看mytop源码,学到的几个小技巧,获益匪浅[*]#!/usr/bin/perl
[*]use strict;
[*]use warnings;
[*]use Data::Dumper;
[*]
[*]my $number = $ARGV;
[*]my $c = $ARGV;
[*]
[*]sub commify($)
[*]{
[*] local $_= shift;
[*] chomp($_);
[*] return 0 unless defined $_;
[*] 1 while s/^(\d+)(\d{3})/$1,$2/ and print Dumper($_); ##,从右边开始,3位一个
[*] return $_;
[*]}
[*]
[*]
[*]sub make_short($)
[*]{
[*] my $number = shift;
[*] my $n = 0;
[*] while ($number > 1_025) { $number /= 1024; $n++; };
[*] return sprintf "%.1f%s", $number, ('','k','M','G', 'T')[$n];##切片
[*]}
[*]
[*]
[*]print commify($number),"\n";
[*]print make_short($number),"\n" if $c;
# perl commify.pl 1230098 y
$VAR1 = '1230,098';
$VAR1 = '1,230,098';
1,230,098
1.2M
# perl commify.pl 1230098
$VAR1 = '1230,098';
$VAR1 = '1,230,098';
1,230,098
fyi : http://jeremy.zawodny.com/mysql/mytop/
页:
[1]