Subject: | Patch: bugfixes and enhancements |
I've sent this patch to Les, the Number::Spell maintainer, three times in the last year. I've received no response. The patch includes the following:
Bugfixes in test.pl:
* Fixed a typo in the EU tests
* Turned on the EU tests, which were mistakenly never performed
Enhancements in Spell.pm and test.pl:
* MANY more tests in test.pl
* support for zero and for negative numbers
* limited but useful support for floating point numbers
My company has used this patch on a web site for six months to write out numbers on simulated checks for online payment. It works very well for us.
I would be happy to take over maintenance of this module, since the author seems unwilling to respond to bug reports.
diff -ur Number-Spell-0.04/Spell.pm Number-Spell-0.04-clotho/Spell.pm
--- Number-Spell-0.04/Spell.pm 2000-03-11 14:20:17.000000000 -0600
+++ Number-Spell-0.04-clotho/Spell.pm 2002-11-07 10:36:34.000000000 -0600
@@ -80,15 +80,14 @@
my %opts=@_;
- if($data=~/(\-?)\s*(\d+)/){
- my ($s,$d)=($1,$2);
- if($d == 0){
- return "zero";
- }
+ if($data=~/(\-?)\s*(\d*)(\.\d*|)/){
+ my ($s,$d,$f)=($1,$2,$3);
+ # remove decimal point if any
+ $f =~ s/^\.//;
+ # correct for empty strings
+ $d=0 if (!$d);
+ $f=0 if (!$f);
my $ret='';
- if($s eq '-'){
- $ret='negative ';
- }
my $l=length($d);
if(defined($opts{Format})&&($opts{Format} eq "eu")){
@@ -180,6 +179,18 @@
}
}
+ if ($ret eq '') {
+ $ret = 'zero';
+ }
+
+ if($s eq '-'){
+ $ret='negative '.$ret;
+ }
+
+ if ($f)
+ {
+ $ret .= " and $f/1" . ("0" x length($f));
+ }
$ret=~s/\s\s+/ /g;
$ret=~s/^\s//g;
diff -ur Number-Spell-0.04/test.pl Number-Spell-0.04-clotho/test.pl
--- Number-Spell-0.04/test.pl 2000-03-11 14:20:12.000000000 -0600
+++ Number-Spell-0.04-clotho/test.pl 2002-11-07 10:38:22.000000000 -0600
@@ -6,7 +6,7 @@
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
-BEGIN { $| = 1; print "1..8\n"; }
+BEGIN { $| = 1; print "1..19\n"; }
END {print "not ok 1\n" unless $loaded;}
use Number::Spell;
$loaded = 1;
@@ -19,18 +19,27 @@
# of the test code):
my %tests =(
+ '0' => 'zero',
'1' => 'one',
'12' => 'twelve',
'523' => 'five hundred twenty three',
'1542' => 'one thousand five hundred forty two',
'5000' => 'five thousand',
'24538000' => 'twenty four million five hundred thirty eight thousand',
- '20000000000' => 'twenty billion');
+ '20000000000' => 'twenty billion',
+ '-0' => 'negative zero',
+ '-1' => 'negative one',
+ '-523' => 'negative five hundred twenty three',
+ '0.0' => 'zero',
+ '1.0' => 'one',
+ '0.23' => 'zero and 23/100',
+ '123.123456789' => 'one hundred twenty three and 123456789/1000000000',
+ );
my %tests_eu =(
'24538000' => 'twenty four million five hundred thirty eight thousand',
'20000000000' => 'twenty thousand million',
- '9512023398683872' => 'nine thousand five hundred twelve billion twenty three thousand three hundred ninty eight million six hundred eighty three thousand eight hundred seventy two'
+ '9512023398683872' => 'nine thousand five hundred twelve billion twenty three thousand three hundred ninety eight million six hundred eighty three thousand eight hundred seventy two'
);
my $c=2;
@@ -49,7 +58,7 @@
}
-foreach (keys %tests_eu){
+foreach $k(keys %tests_eu){
my $r=spell_number($k,Format=>'eu');
if($r eq $tests_eu{$k}){
@@ -58,7 +67,7 @@
print "not ok $c\n";
print " $k (eu) spelled to\n";
print " \"$r\" should have spelled to\n";
- print " \"$tests{$k}\"\n";
+ print " \"$tests_eu{$k}\"\n";
}
$c++;
}