Lingua-EN-Nums2Words
view release on metacpan or search on metacpan
Nums2Words.pm view on Meta::CPAN
###############################################################################
# Numbers to Words Module for Perl.
# Copyright (C) 1996-2016, Lester Hightower <hightowe@cpan.org>
###############################################################################
package Lingua::EN::Nums2Words;
require 5.000;
require Exporter;
use strict;
use warnings;
our @ISA=qw(Exporter);
our @EXPORT=qw(num2word num2usdollars num2word_ordinal num2word_short_ordinal);
our $VERSION = "1.16";
sub Version { $VERSION; }
###############################################################################
# Private File-Global Variables ###############################################
###############################################################################
# Initialization Function init_mod_vars() sets up these variables
my @Classifications;
my @MD;
my @Categories;
my %CardinalToOrdinalMapping;
my @CardinalToShortOrdinalMapping;
my $word_case = 'upper';
# At module load time, initialize our static, file-global variables.
# We use these file-global variables to increase performance when one
# needs to compute many iterations for numbers to words. The alternative
# would be to re-instantiate the never-changing variables over and over.
&init_mod_vars;
###############################################################################
###############################################################################
# Public Functions ############################################################
###############################################################################
sub set_case($) {
my $case=lc(shift @_);
my @cases=qw(upper lower);
if (scalar(grep(/^$case$/, @cases)) != 1) {
die __PACKAGE__.":set_case() only accepts these optons: " .
join(", ", @cases) . "\n";
}
$word_case=$case;
}
sub case($) {
my $str=shift @_;
if ($word_case eq 'upper') {
return uc($str);
} elsif ($word_case eq 'lower') {
return lc($str);
} else {
retrn $str;
}
}
sub num2word {
my $Number = shift(@_);
return(case(&num2word_internal($Number, 0)));
}
sub num2usdollars {
my $Number = shift(@_);
# OK, lets do some parsing of what we were handed to be nice and
# flexible to the users of this API
$Number=~s/^\$//; # Kill leading dollar sign
# Get the decimal into hundreths
# NOTE: sprintf(%f) fails on very large decimal numbers, so we use
# our RoundToTwoDecimalPlaces() instead of a sprintf() call.
#$Number=sprintf("%0.02f", $Number);
$Number = RoundToTwoDecimalPlaces($Number);
# Get the num2word version
my $Final=num2word_internal($Number, 1);
# Now whack the num2word version into a US dollar version
my $dollar_verb='DOLLAR';
if (abs(int($Number)) != 1) { $dollar_verb .= 'S'; }
if (! ($Final=~s/ AND / $dollar_verb AND /)) {
$Final .= ' DOLLAR';
if (abs($Number) != 1) { $Final .='S'; }
} else {
$Final=~s/(HUNDREDTH|TENTH)([S]?)/CENT$2/;
}
# Return the verbiage to the calling program
return(case($Final));
}
( run in 0.454 second using v1.01-cache-2.11-cpan-4ab04211f4c )