SYNOPSIS

   use Lingua::ES::Numeros ":constants";

   my $obj = new Lingua::ES::Numeros ('MAYUSCULAS' => 1);
   print $obj->cardinal(124856), "\n";
   print $obj->real(124856.531), "\n";
   $obj->{GENERO} = FEMALE;
   print $obj->ordinal(124856), "\n";

DESCRIPTION

This module supports the translation of cardinal, ordinal and, real numbers, the module handles integer numbers up to vigintillions (that's 1e120), since Perl does not handle such numbers natively, numbers are kept as text strings because processing does not justify using bigint.

Currently Lingua::ES::Numeros handles numbers up to 1e127-1 (999999 vigintillions).

METHODS

\s-1CONSTRUCTOR:\s0 new

To create a new Lingua::ES::Numeros, use the new class method. This method can receive as parameters any of the above mentioned fields.

Examples:

use Lingua::ES::Numeros ':constants';

# Use the fields' default values $obj = new Lingua::ES::Numeros;

# Specifies the values of some of them $obj = Lingua::ES::Numeros->new( ACENTOS => 0, MAYUSCULAS => 1, GENERO => FEMALE, DECIMAL => ',', SEPARADORES=> '"_' );

\s-1DECIMAL\s0

Specifies the character string that will be used to separate the integer from the fractional part of the number to convert. The default value for \s-1DECIMAL\s0 is '.'

\s-1SEPARADORES\s0

Character string including all of the format characters used when representing a number. All of the characters in this string will be ignored by the parser when analyzing the number. The default value for \s-1SEPARADORES\s0 is '_'

\s-1ACENTOS\s0

Affects the way in which the generated string for the translated numbers is given; if it is false, the textual representation will not have any accented characters. The default value for this field is true (with accents).

\s-1MAYUSCULAS\s0

If this is a true value, the textual representation of the number will be an uppercase character string. The default value for this field is false (lowercase).

\s-1HTML\s0

If this is a true value, the textual representation of the number will be a HTML-valid string character (accents will be represented by their respective \s-1HTML\s0 entities). The default value is 0 (text).

\s-1GENERO\s0

The gender of the numbers can be \s-1MALE\s0, \s-1FEMALE\s0 or \s-1NEUTRAL\s0, respectively for femenine, masculine or neutral numbers. The default value is \s-1MALE\s0. The following table shows the efect of \s-1GENDER\s0 on translation of Cardinal and Ordinal numbers: +---+---------------------+-----------------------------+ | N | CARDINAL | ORDINAL | | u +------+------+-------+---------+---------+---------+ | m | MALE |FEMALE|NEUTRAL| MALE | FEMALE | NEUTRAL | +---+------+------+-------+---------+---------+---------+ | 1 | uno | una | un | primero | primera | primer | | 2 | dos | dos | dos | segundo | segunda | segundo | | 3 | tres | tres | tres | tercero | tercera | tercer | +---+------+------+-------+---------+---------+---------+ The constants \s-1MALE\s0, \s-1FEMALE\s0 and \s-1NEUTRAL\s0 and their spanish counterparts \s-1MASCULINO\s0, \s-1FEMENINO\s0 and \s-1NEUTRO\s0, may be imported with the tag \*(L":constants\*(R" on module use.

\s-1SEXO\s0

Deprecated option only for backward compatibility, use \s-1GENERO\s0 instead.

\s-1UNMIL\s0

This field affects only the translation of cardinal numbers. When it is a true value, the number 1000 is translated to 'un mil' (one thousand), otherwise it is translated to the more colloquial 'mil' (thousand). The default value is 1.

\s-1NEGATIVO\s0

Contains the character string with the text to which the negative sign (-) will be translated with. Defaults to 'menos'. For example: default translation of -5 will yield \*(L"menos cinco\*(R".

\s-1POSITIVO\s0

Contains the character string with the text to which the positive sign will be translated with. Defaults to ''. For example: default translation of 5 will yield \*(L"cinco\*(R".

\s-1FORMATO\s0

A character string specifying how the decimals of a real number are to be translated. Its default value is 'con %2d ctms.' (see the real method)

Aliases

All the options have the following english aliases.

English Option name -------------------------- ACCENTS ACENTOS UPPERCASE MAYUSCULAS SEPARATORS SEPARADORES GENDER GENERO POSITIVE POSITIVO NEGATIVE NEGATIVO FORMAT FORMATO

cardinal

\s-1SYNOPSIS:\s0

  $text = $obj->cardinal($num)

Parameters the number.

Description

Translates a cardinal number ($num) to spanish text, translation is performed according to the following object ($obj) settings: \s-1DECIMAL\s0, \s-1SEPARADORES\s0, \s-1SEXO\s0, \s-1ACENTOS\s0, \s-1MAYUSCULAS\s0, \s-1POSITIVO\s0 and \s-1NEGATIVO\s0.

This method ignores any fraction part of the number ($num).

Return Value

Textual representation of the number as a string

real

\s-1SYNOPSIS:\s0

  $text = real($n; $genf, $genm)

Translates the real number ($n) to spanish text.

The optional $genf and $genm parameters are used to specify gender of the fraction part and fraction part magnitude in that order. If $genf is missing it will default to the \s-1GENDER\s0 option, and $genm will default to the $genf's value.

This translation is affected by the options: \s-1DECIMAL\s0, \s-1SEPARADORES\s0, \s-1GENDER\s0, \s-1ACENTOS\s0, \s-1MAYUSCULAS\s0, \s-1POSITIVO\s0, \s-1NEGATIVO\s0 and \s-1FORMATO\s0.

Fraction format (\s-1FORMATO\s0)

\s-1FORMAT\s0 option is a formatting string like printf, it is used to format the fractional part before appending it to the integer part. It has the following format specifiers: Formats the fractional part as text with precisiA\*~Xn of N digits, for example: number '124.345' formated with string '\s-1CON\s0 %s.' will yield the text 'ciento veinticuatro \s-1CON\s0 trescientas cuarenta y cinco mile\*'simas', and formatted with string '\s-1CON\s0 %2s.' will yield 'ciento veinticuatro \s-1CON\s0 treinta y cuatro cente\*'simas'. Formats the fractional part as a number (no translation), with precision of N digits, veri similar to sprintf's %d format, for example: number '124.045' formated with '\s-1CON\s0 %2d ctms.' will yield: 'ciento veinticuatro \s-1CON\s0 04 ctms.'

ordinal

\s-1SYNOPSIS:\s0

  $text = $obj->ordinal($num)

Parameters the number.

Description

Translates an ordinal number ($num) to spanish text, translation is performed according to the following object ($obj) settings: \s-1DECIMAL\s0, \s-1SEPARADORES\s0, \s-1GENERO\s0, \s-1ACENTOS\s0, \s-1MAYUSCULAS\s0, \s-1POSITIVO\s0 and \s-1NEGATIVO\s0.

This method croacks if $num <= 0 and carps if $num has a fractional part.

Return Value

Textual representation of the number as a string

Accessors

Each of the options has a setter/getter with the name of the option in lowercase, all the accessors have the following sintax:

Getters

$obj->name_of_option()

Returns the current value of the option.

Setters

$obj->name_of_option( $value )

Sets the option to $value and returns $obj

List of accessors

$obj->accents $obj->acentos $obj->uppercase $obj->mayusculas $obj->unmil $obj->html $obj->decimal $obj->separators $obj->separadores $obj->gender $obj->genero $obj->positive $obj->positivo $obj->negative $obj->negativo $obj->format $obj->formato

INTERNALS

Functions in this secction are generally not used, but are docummented here for completeness.

This is not part of the module's \s-1API\s0 and is subject to change.

\s-1CARDINAL\s0 \s-1SUPPORT\s0

Construction of cardinal numbers

cardinal_e2

\s-1SYNOPSIS\s0

cardinal_e2($n, $nn)

\s-1PARAMETERS\s0

the number. word stack.

\s-1DESCRIPTION\s0

This procedure takes $n (an integer in the range [0 .. 99], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.

cardinal_e3

\s-1SYNOPSIS\s0

cardinal_e3($n, $nn)

\s-1PARAMETERS\s0

the number. word stack.

\s-1DESCRIPTION\s0

This procedure takes $n (an integer in the range [0 .. 99], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.

cardinal_e6

\s-1SYNOPSIS\s0

cardinal_e6($n, $nn, $mag, $un_mil, $postfix)

\s-1PARAMETERS\s0

the number. word stack. magnitude of the number 1 for millions, 2 for billions, etc. if true 1000 is translated as \*(L"un mil\*(R" otherwise \*(L"mil\*(R" array representing plural & singular magnitude of the number, in this order.

\s-1DESCRIPTION\s0

This procedure takes $n, and pushes the numbers text translation into $nn, on a word by word basis, with the proper translated magnitude. If $n == 0 nothing is pushed into $nn.

cardinal_generic

\s-1SYNOPSIS\s0

cardinal_generic($n, $exp, $fmag, $gen)

\s-1PARAMETERS\s0

the number. exponent. closure to format the 6 digits groups. gender of the magnitude (optional defaults to \s-1NEUTRAL\s0):

    \s-1FEMALE\s0  for female gender (1 -> una).
    \s-1MALE\s0    for male gender (1 -> uno).
    \s-1NEUTRAL\s0 for neutral gender (1 -> un).
\s-1DESCRIPTION\s0

This function translate the natural number $n to spanish words, adding gender where needed.

\s-1RETURN\s0 \s-1VALUE\s0

Translation of $n to spanish text as a list of words.

cardinal_simple

\s-1SYNOPSIS\s0

cardinal_simple($n, $exp, $un_mil; $gen)

\s-1PARAMETERS\s0

the number. exponent. if true 1000 is translated as \*(L"un mil\*(R" otherwise \*(L"mil\*(R" gender of the magnitude (optional defaults to \s-1NEUTRAL\s0):

    \s-1FEMALE\s0  for female gender (1 -> una).
    \s-1MALE\s0    for male gender (1 -> uno).
    \s-1NEUTRAL\s0 for neutral gender (1 -> un).
\s-1DESCRIPTION\s0

This function translate the natural number $n to spanish words, adding gender where needed. This procedure just builds a closure with format information, to call cardinal_e6, and then calls cardinal_generic to do the work.

\s-1RETURN\s0 \s-1VALUE\s0

Translation of $n to spanish text as a list of words.

fraccion_mag_prefix

\s-1SYNOPSIS\s0

fraccion_mag_prefix($mag, $gp)

\s-1PARAMETERS\s0

the number. exponent. magnitude of the number 1 for millionths, 2 for billionths, etc. gender and plural of the number, is the concatenation of gender and plural gender must be one of \s-1FEMALE\s0, \s-1MALE\s0 or \s-1NEUTRAL\s0, and plural must be '' for singular and 's' for plural. Note that \s-1NEUTRAL\s0 + plural is a nonsense. gender of the number (same values as $gen).

\s-1DESCRIPTION\s0

This function returns the name of the magnitude of a fraction, $mag is the number of decimal digits. For example 0.001 has $mag == 3 and translates to \*(L"milesimos\*(R" if $gp is (\s-1MALE\s0 . 's').

\s-1RETURN\s0 \s-1VALUE\s0

Translation of $n to spanish text as a string.

fraccion_simple

\s-1SYNOPSIS\s0

fraccion_simple($n, $exp, $un_mil, $gen; $ngen)

\s-1PARAMETERS\s0

the number. exponent. if true 1000 is translated as \*(L"un mil\*(R" otherwise \*(L"mil\*(R" gender of the magnitude (optional defaults to \s-1NEUTRAL\s0):

    \s-1FEMALE\s0  for female gender (1 -> primera).
    \s-1MALE\s0    for male gender (1 -> primero).
    \s-1NEUTRAL\s0 for neutral gender (1 -> primer).

gender of the number (same values as $gen).

\s-1DESCRIPTION\s0

This function translate the fraction $n to spanish words, adding gender where needed. This procedure just builds a closure with format information, to call cardinal_e6, and then calls cardinal_generic to do the work.

\s-1RETURN\s0 \s-1VALUE\s0

Translation of $n to spanish text as a list of words.

\s-1ORDINAL\s0 \s-1SUPPORT\s0

Construction of ordinal numbers

ordinal_e2

\s-1SYNOPSIS\s0

ordinal_e2($n, $nn)

\s-1PARAMETERS\s0

the number. word stack.

\s-1DESCRIPTION\s0

This procedure takes $n (an integer in the range [0 .. 99], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.

ordinal_e3

\s-1SYNOPSIS\s0

ordinal_e3($n, $nn)

Parameters

the number. word stack.

\s-1DESCRIPTION\s0

This procedure takes $n (an integer in the range [0 .. 999], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.

ordinal_e6

\s-1SYNOPSIS\s0

ordinal_e6($n, $nn, $mag, $un_mil, $postfix)

\s-1PARAMETERS\s0

the number. word stack. magnitude of the number 1 for millions, 2 for billions, etc.

\s-1DESCRIPTION\s0

This procedure takes $n, and pushes the numbers text translation into $nn, on a word by word basis, with the proper translated magnitude. If $n == 0 nothing is pushed into $nn.

ordinal_simple

\s-1SYNOPSIS\s0

ordinal_simple($n, $exp; $gen)

\s-1PARAMETERS\s0

the number. exponent. if true 1000 is translated as \*(L"un mil\*(R" otherwise \*(L"mil\*(R" gender of the magnitude (optional defaults to \s-1NEUTRAL\s0):

    \s-1FEMALE\s0  for female gender (1 -> primera).
    \s-1MALE\s0    for male gender (1 -> primero).
    \s-1NEUTRAL\s0 for neutral gender (1 -> primer).
\s-1DESCRIPTION\s0

This function translate the fraction $n to spanish words, adding gender where needed. This procedure just builds a closure with format information, to call ordinal_e6, and then calls ordinal_generic to do the work.

\s-1RETURN\s0 \s-1VALUE\s0

Translation of $n to spanish text as a list of words.

\s-1MISCELANEOUS\s0

Everithing not fitting elsewere

parse_num

\s-1SYNOPSIS\s0

parse_num($num, $dec, $sep) Decomposes the number in its constitutive parts, and returns them in a list: use Lingua::ES::Numeros; ($sgn, $ent, $frc, $exp) = parse_num('123.45e10', '.', '",');

\s-1PARAMETERS\s0

the number to decompose decimal separator (tipically ',' or '.'). separator characters ignored by the parser, usually to mark thousands, millions, etc..

\s-1RETURN\s0 \s-1VALUE\s0

This function parses a general number and returns a list of 4 elements:

sign of the number: -1 if negative, 1 otherwise integer part of the number decimal (fraction) part of the number exponent of the number

Croaks if there is a syntax error.

retval

\s-1SYNOPSIS\s0

$obj->retval($value)

\s-1DESCRIPTION\s0

Utility method to adjust return values, transforms text following the options: \s-1ACENTOS\s0, \s-1MAYUSCULAS\s0 y \s-1HTML\s0. Returns the adjusted $value.

DEPENDENCIES

Perl 5.006, Exporter, Carp

RELATED TO Lingua::ES::Numeros…

http://roble.pntic.mec.es/~msanto1/ortografia/numeros.htm

AUTHOR

Jose Rey, <[email protected]>

COPYRIGHT AND LICENSE

Copyright (C) 2001-2009 by Jose Rey

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.