6

I need some code in my program which takes a number as input and converts it into corresponding text e.g. 745 to "seven hundred forty five".

Now, I can write code for this, but is there any library or existing code I can use?

brian d foy
  • 129,424
  • 31
  • 207
  • 592

5 Answers5

18

From perldoc of Lingua::EN::Numbers:

use Lingua::EN::Numbers qw(num2en num2en_ordinal);

my $x = 234;
my $y = 54;
print "You have ", num2en($x), " things to do today!\n";
print "You will stop caring after the ", num2en_ordinal($y), ".\n";

prints:

You have two hundred and thirty-four things to do today!
You will stop caring after the fifty-fourth.
2

You need to look at this stackoverflow question

From the above-mentioned link:

perl -MNumber::Spell -e 'print spell_number(2);'
Community
  • 1
  • 1
Aamir
  • 14,882
  • 6
  • 45
  • 69
1

Take a look at the Math::BigInt::Named module.

innaM
  • 47,505
  • 4
  • 67
  • 87
1

You can try something like this:

#!/usr/bin/perl

use strict;
use warnings;

my %numinwrd = (
  0 => 'Zero', 1 => 'One', 2 => 'Two',   3 => 'Three', 4 => 'Four',
  5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight', 9 => 'Nine',
);

print "The number when converted to words is 745=>".numtowrd(745)."\n";

sub numtowrd {
  my $num = shift;
  my $txt = "";  
  my @val = $num =~ m/./g;

  foreach my $digit (@val) {    
    $txt .= $numinwrd{$digit} . " ";   
  }

  return $txt;
}

The output is:

The number when converted to words is 745=>Seven Four Five 
amon
  • 57,091
  • 2
  • 89
  • 149
user1613245
  • 343
  • 5
  • 14
  • To convert `@val` to `$txt`, it might be easier to do `$txt = join " ", map {$numinwrd{$_}} @val`, effectively making your sub a one-liner. Also, this solution doesn't produce `seven hundred forty five`. – amon Oct 18 '12 at 11:31
  • You can give the code if you can make the output to seven hundred forty five – user1613245 Oct 19 '12 at 04:20
0

Also take a look at Nums2Words.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Anand Shah
  • 14,575
  • 16
  • 72
  • 110