簡體   English   中英

如何在Perl中將數字轉換為文本?

[英]How can I convert a number to text in Perl?

我需要在我的程序中使用一些代碼作為輸入並將其轉換為相應的文本,例如745到“七百四十五”。

現在,我可以為此編寫代碼,但是我可以使用任何庫或現有代碼嗎?

來自Lingua的 perldoc :: EN ::數字

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.

您需要查看此stackoverflow問題

從上述鏈接:

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

看一下Math :: BigInt :: Named模塊。

你可以嘗試這樣的事情:

#!/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 

另請參閱Nums2Words

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM