Search code examples
perlperl-module

Perl convert string to numeric number


I want to convert a string ordinal to a number in perl I have searched but not get exact answer.

Example: if the input is

one it should be 1.
five hundred it should be 500.
three hundred it should be 300.

Is there any module to do this?


Solution

  • One of the best parts of Perl is CPAN and, sure enough, a couple minutes of poking around on metacpan turned up the Lingua::EN::Words2Nums module:

    use Lingua::EN::Words2Nums;
    $num=words2nums("two thousand and one");
    $num=words2nums("twenty-second");
    $num=words2nums("15 billion, 6 million, and ninteen");