[comp.lang.perl] Currency separators.

tony@oha.UUCP (Tony Olekshy) (03/18/90)

The following routine puts commas in numbers, eg: 1234567.12 -> 1,234,567.12

sub commas {
    while ($_ =~ s/^([^.]*)([\d])([\d]{3})([,.].*)?$/\1\2,\3\4/) {}
    $_;
    }

sub test { print &commas($_),"\n"; }

grep(&test($_), split(/\n/,<<end));
1
12
123
1234
12345
123456
1234567
12345678
1.1
12.12
123.123
1234.1234
12345.12345
123456.123456
1234567.1234567
12345678.12345678
end

--
Yours, etc., Tony Olekshy (...!alberta!oha!tony or tony@oha.UUCP).