如何在Perl中将数字转换为文本?

时间:2009-08-18 09:01:43

标签: perl numbers

我需要在我的程序中使用一些代码作为输入并将其转换为相应的文本,例如745至“七百四十五”。

现在,我可以为此编写代码,但是我可以使用任何库或现有代码吗?

5 个答案:

答案 0 :(得分:18)

来自Lingua::EN::Numbers的perldoc:

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";

打印:

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

答案 1 :(得分:2)

您需要查看this stackoverflow question

从上述链接:

perl -MNumber::Spell -e 'print spell_number(2);'

答案 2 :(得分:1)

查看Math::BigInt::Named模块。

答案 3 :(得分:1)

您可以尝试这样的事情:

#!/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 number when converted to words is 745=>Seven Four Five 

答案 4 :(得分:0)

另请查看Nums2Words