Reference

Numeros(3pm)

Lingua::ES::Numeros - Converts numbers to Spanish text

   use Lingua::ES::Numeros;

   $obj = new Lingua::ES::Numeros ('MAYUSCULAS' => 1);
   print $obj->Cardinal(124856), "\n";
   print $obj->Real(124856.531), "\n";
   $obj->{SEXO} = 'a';
   print $obj->Ordinal(124856), "\n";

Perl 5.004, Exporter, Carp Both this module's interface and documentation were originally written completely in Spanish. The English documentation was written by Gunnar Wolf <[email protected]> for its inclusion in the Debian packaging. Obviously, in the documentation, I will still reflect the Spanish method and attribute names - it might sound strange, but it's the only way. If you need something more natural, it'd be almost trivial to call a wrapper for this module using English method and attribute names. Lingua::ES::Numeros converts arbitrary precision numbers to their textual representation in Spanish. It can produce the representation of cardinal, ordinal and real numbers. As the handled numbers have a greater range than the Perl native numeric type, they are handled as character strings, which allows for the unlimited growth of the conversion system. Decomposes the number in its constitutive parts, and returns them in a list:

   use Lingua::ES::Numeros qw( :All );
   ($sgn, $ent, $frc, $exp) = parse_num('123.45e10', '.', '",');

Parameters The number to decompose The decimal separator The separator for thousands, millions, etc.
Return values Sign, it can be -1 if the negative sign is present, 1 if the positive sign is present, and 0 if there is no explicit sign. Integer part of the number, only the most significant digits (see $exp) Fractional part of the number, only the least significant digits (see $exp) Number's exponent, if it is > 0, it means the number of zeros that follow the integer part, if it is <0, it means the number of zeros that are between the decimal point and the fractional part.

This method is not implicitly exported, so it must be imported by either of:

   use Lingua::ES::Numeros qw(parse_num);
   use Lingua::ES::Numeros qw(:All);

The object has the following fields, which alter the way in which the conversion is carried out: 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 DECIMAL 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 SEPARADORES 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 1 (with accents). This field can be very useful if the character set used in your locale is not Latin1, as this module's output will be given in this encoding (see BUGS). 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 0 (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 HTML entities). The default value is 0 (text). The sex of the numbers can be 'a', 'o' or '', respectively for femenine, masculine or neutral numbers. The default value is 'o'.

 +---+--------------------+-----------------------------+
 |NU |      CARDINAL      |           ORDINAL           |
 |MB +------+------+------+---------+---------+---------+
 |ER | 'o'  | 'a'  |  ''  |   'o'   |   'a'   |   ''    |
 +---+------+------+------+---------+---------+---------+
 | 1 | uno  | una  | un   | primero | primera | primer  |
 | 2 | dos  | dos  | dos  | segundo | segunda | segundo |
 | 3 | tres | tres | tres | tercero | tercera | tercer  |
 +---+------+------+------+---------+---------+---------+

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'. Contains the character string with the text to which the positive sign will be translated with. Defaults to ''. This string is only added when the sign '+' is explicitly received, otherwise it is not added even if the number is assumed as positive. A character string specifying how the decimals of a real number are to be translated. Its default value is 'con %02d ctms.' (see the real method) 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;

      # 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,
                                         'SEXO'       => 'a',
                                         'DECIMAL'    => ',',
                                         'SEPARADORES'=> '"_' );

Converts the $n number to its Spanish cardinal representation.

This conversion is affected by the DECIMAL, SEPARADORES, SEXO, ACENTOS, MAYUSCULAS, POSITIVO and NEGATIVO fields.

This conversion ignores the fractional part of the number, if present. Converts the $n number, as a real number, to its Spanish representation.

The optional parameter $fsexo is used to specify a different sex for the decimal part. It accepts the same values that the SEXO field, but the neutral sex becomes masculine for the fractional part. If it is not specified, the value of the SEXO field will be used.

This conversion is affected by the DECIMAL, SEPARADORES, SEXO, ACENTOS, MAYUSCULAS, POSITIVO and NEGATIVO fields.
Format for the fractional part (FORMATO) This conversion also uses the FORMATO field to guide the fractional part's conversion. This field is a sprintf-like format field, which has only a single format specification preceded by '%'. It currently handles only two valid specifications: Includes the textual representation of the fractional part of the format. For example, converting '123.345' with a 'mas %s.' format will result in: 'CIENTO VEINTITRES Y TRECIENTOS CUARENTA MAS CINCO MILESIMAS'. Includes the numeric representation of the fractional part, with N as a valid number for the '%d' format of sprintf. For example, converting '123.345' with a 'con %02d ctms.' results in: 'CIENTO VEINTITRES Y TRECIENTOS CUARENTA CON 34 CTMS.' Converts the $n number, as an ordinal number, to Spanish.

The conversion is affected by the DECIMAL, SEPARADORES, SEXO, ACENTOS and MAYUSCULAS fields.

It will issue a warning if the number is negative and/or if it is not a positive nonzero natural. The number has a syntax error The integer part of the number is too large. Currently, only numbers up to 10**126 - 1 are handled, as I don't know what is the textual representation of numbers >= 10**126. Any help or correction will be most welcome. the fractional part of the number is smaller than 10**-126 and can not be translated for the above mentioned reasons. The number requested to be converted into an ordinal is negative. The number requested to be converted into an ordinal has decimals. Jose Luis Rey Barreira <[email protected]> The uppercase conversion is carried out by a transliteration, in order to be able to convert accented characters. The problem is this will not work if the character set is not ISO 8859-1 (Latin1) or ISO 8859-15.

The alternatives for this problem would be: Using Perl 5.6 or higher with 'utf8'. This would however restrict the module's usage to a large amount of users who still prefer Perl 5.00x. On the other hand it would allow it to use locales, although I'm not sure if they work the same way in Unix, Windows, BeOS, etc., so I think the transliteration is adequate for now. This code is intellectual property of Jose Rey and is distributed according to the terms of the GNU General Public License, whose letter and explanation can be found in English in the http://www.gnu.org/licenses/licenses.html web page, and for which a Spanish translation is available at http://lucas.hispalinux.es/Otros/gples/gples.html