Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Spelled out numbers to numerical equivalent function

by Akira71 (Scribe)
on Apr 12, 2005 at 17:53 UTC ( [id://447119]=note: print w/replies, xml ) Need Help??


in reply to Spelled out numbers to numerical equivalent function

Ahh, now it looks as though I spoke too soon. For instance in a case like this test below:
#!/usr/bin/perl + + use Lingua::EN::Numericalize; + + print str2nbr('First, Second, Third, Twentythird');


It prints '123' not what I would want to print which is:
'1st, 2nd, 3rd, 23rd'
It seems as if this problem is still a bit stickier. At least the package is a step in the right direction.

Thank you -- Akira the humble

Replies are listed 'Best First'.
Re^2: Spelled out numbers to numerical equivalent function
by mayhem (Hermit) on Apr 12, 2005 at 18:55 UTC
    There is a easy way to remedy this...
    for my $text (qw/first third thirteenth twentyfirst twentysecond oneh +undredone onehundredtwelve onehundredeleven/) { my $number = str2nbr($text); my $sufix = 'th'; if ( $number =~ /^1$/ or $number =~ /[^1]1$/ ) { $sufix = 'st'; } if ( $number =~ /^2$/ or $number =~ /[^1]2$/ ) { $sufix = 'nd'; } if ( $number =~ /^3$/ or $number =~ /[^1]3$/ ) { $sufix = 'rd'; } print $number . $sufix, $/; }
    and i get 1st, 3rd, 13th, 21st, 22nd, 101st, 112th, 111th I believe this will work for all your cases, i would test it more though. ED: changed a var name, didn't like it after i looked at again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found