Translates numbers to spanish text
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";
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).
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=> '"_' );
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 '.'
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 '_'
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).
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).
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).
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.
Deprecated option only for backward compatibility, use \s-1GENERO\s0 instead.
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.
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".
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".
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
\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
\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.'
\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
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
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.
Construction of cardinal numbers
cardinal_e2
cardinal_e2($n, $nn)
the number. word stack.
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
cardinal_e3($n, $nn)
the number. word stack.
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
cardinal_e6($n, $nn, $mag, $un_mil, $postfix)
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.
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
cardinal_generic($n, $exp, $fmag, $gen)
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).
This function translate the natural number $n to spanish words, adding gender where needed.
Translation of $n to spanish text as a list of words.
cardinal_simple
cardinal_simple($n, $exp, $un_mil; $gen)
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).
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.
Translation of $n to spanish text as a list of words.
fraccion_mag_prefix
fraccion_mag_prefix($mag, $gp)
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).
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').
Translation of $n to spanish text as a string.
fraccion_simple
fraccion_simple($n, $exp, $un_mil, $gen; $ngen)
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).
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.
Translation of $n to spanish text as a list of words.
Construction of ordinal numbers
ordinal_e2
ordinal_e2($n, $nn)
the number. word stack.
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
ordinal_e3($n, $nn)
the number. word stack.
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
ordinal_e6($n, $nn, $mag, $un_mil, $postfix)
the number. word stack. magnitude of the number 1 for millions, 2 for billions, etc.
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
ordinal_simple($n, $exp; $gen)
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).
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.
Translation of $n to spanish text as a list of words.
Everithing not fitting elsewere
parse_num
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', '.', '",');
the number to decompose decimal separator (tipically ',' or '.'). separator characters ignored by the parser, usually to mark thousands, millions, etc..
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
$obj->retval($value)
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.
Perl 5.006, Exporter, Carp
http://roble.pntic.mec.es/~msanto1/ortografia/numeros.htm
Jose Rey, <[email protected]>
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.