Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl: the Markov chain saw
 
PerlMonks  

Number::Spell

by PERLscienceman (Curate)
on Jan 28, 2004 at 01:07 UTC ( #324591=modulereview: print w/ replies, xml ) Need Help??

Item Description: Perl extension for spelling out numbers.

Review Synopsis:

CPAN LINK: Number::Spell
CPAN ABSTRACT: Number::Spell provides functionality for spelling out numbers. Currently only integers are supported.
Introduction:
Every so often I get the urge to troll around the CPAN Module Repository to see what is interesting. This module caught my eye as a 'Cool Use for Perl', so I thought I would download it and give it a try.
Functionality:
Number::Spell, upon initial tinkering does what it says spells out integers into english words. I tested it out on some 'smaller' numbers (see demo below) and it seemed to work fine. So... I thought to myself lets see if we can break it, after all the documentation say it can go into the 'vigintillions' Unfortunately, I was successful at breaking it. I hit the ceiling at 100 trillion, after adding one more zero, which would then be one quadrillion, it only returned 'one'.
Demo Code:

#!/usr/bin/perl -w use strict; use Number::Spell; my $string=spell_number(8597); print "$string\n"; ----- Result: eight thousand five hundred ninety seven
Bug(s) Found: Stops working properly after 100 Trillion.
Final Thoughts:
I think this would be a really cool module if it worked properly past 100 Trillion. Has anyone else tried it and had the same result? Another possiblity would be for interpreting reals, although that would probably be pretty dicey to implement especially with a number like 1.09873532 . The author did mention this possibility in the original version.
UPDATE:
I did a little digging into the module and determined that when you go one place above 100 Trillon (1000000000000000), the number then get interpreted or changed into exponential format by the interpreter so instead of 1 Quadrillion as 10000000000000000, it is 1e16, which is not recognized/proper split by the regex inside the if statement on line 83 of spell.pm .
if($data=~/(\-?)\s*(\d+)/){
With a little more tinkering/experimentation with the demo script I have determined that such a large number will work with the module if you send it as a quoted string.
my $string=spell_number('1000000000000000'); #this works my $string=spell_number(1000000000000000); #this doesNOT
The ultimate solution would be to fix the regex on line 83 in spell.pm to deal with the exponential format. Unfortunately a regex wizard I am not. So.... I would be inclined to leave that in the hands of the author or someone who knows more of regexes than I do. :)

Comment on Number::Spell
Select or Download Code
Re: Number::Spell
by blokhead (Monsignor) on Jan 28, 2004 at 14:26 UTC
    This is an issue with Perl, not with this module. Huge numbers get converted to floats automatically by Perl. You can't convert back, as you suggest, because you've lost accuracy:
    $ perl -e 'print 10000000000000001, $/' 1e+16 $ perl -e 'print 10000000000000005, $/' 1e+16 $ perl -e 'print 10000000000000000, $/' 1e+16
    You will find that every module on CPAN that needs to deal with integers this big either (A) requires you to pass the number as a string, or (B) uses Math::BigInt or similar, to prevent Perl from converting the integer to a float. Even notice from Math::BigInt's pod:
    Scalars holding numbers may also be passed, but note that non-integer +numbers may already have lost precision due to the conversation to float. Quot +e your input if you want BigInt to see all the digits: $x = Math::BigInt->new(12345678890123456789); # bad $x = Math::BigInt->new('12345678901234567890'); # good

    blokhead

Re: Number::Spell
by halley (Prior) on Feb 05, 2004 at 16:51 UTC

    I have some code for this, and I'd like to compare it to yours tonight when I get home. I should also adopt the stringy method to avoid the platform-specific integer issues.

    It appears your version doesn't put commas between groups, e.g., "eight thousand, five hundred ninety seven".

    You also only spell cardinal numbers. How about ordinal numbers, e.g., "six thousand, twenty-first"?

    --
    [ e d @ h a l l e y . c c ]

Back to Reviews

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: modulereview [id://324591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls