Lingua...Numeros(3pm)

πŸ“¦
version 0.09 Β· 2 versions
  • 0.09 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.09 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Lingua::ES::Numeros πŸ’–

okay bestie, come closer 🫢 this is the page for Lingua::ES::Numeros, and it lives in the User Contributed Perl Documentation corner of the manual (yes, the 3pm shelf, iykyk). she turns numbers into spanish text and honestly? she ate πŸ’…


🌸 NAME 🌸

Lingua::ES::Numeros β€” Translates numbers to spanish text.

that's the whole job description and she does it flawlessly, no cap ✨


✨ 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 πŸ¦‹

okay so here's the tea β˜• this module supports the translation of cardinal, ordinal and real numbers. and she does integer numbers up to vigintillions β€” that's 1e120 bestie, absolutely unhinged, I'm obsessed 😭

and here's the thing that makes her different: since Perl does not handle such numbers natively, the numbers are kept as text strings, because processing does not justify using bigint. (she said "why would I pull in a whole bigint situation for this, be serious" πŸ’…)

Currently Lingua::ES::Numeros handles numbers up to 1e127-1 (999999 vigintillions). yeah. 999999 of them. main character energy fr fr πŸ‘‘


πŸ‘‘ METHODS πŸ‘‘

πŸŽ€ CONSTRUCTOR: new

to create a new Lingua::ES::Numeros, you 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=> '"_' );

and now the fields themselves, one by one, she's thorough like that 🌸

πŸŽ€ DECIMAL

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 '.'

πŸ”€ SEPARADORES

Character string including all of the format characters used when representing a number. Every single character in this string will be ignored by the parser when analyzing the number. The default value for SEPARADORES is '_'

🌈 ACENTOS

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).

πŸ”  MAYUSCULAS

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).

🌐 HTML

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).

🧚 GENERO

The gender of the numbers can be MALE, FEMALE or NEUTRAL, respectively for femenine, masculine or neutral numbers. The default value is MALE.

The following table shows the efect of GENDER on translation of Cardinal and Ordinal numbers:

  • 1 β€” CARDINAL: MALE uno, FEMALE una, NEUTRAL un; ORDINAL: MALE primero, FEMALE primera, NEUTRAL primer
  • 2 β€” CARDINAL: MALE dos, FEMALE dos, NEUTRAL dos; ORDINAL: MALE segundo, FEMALE segunda, NEUTRAL segundo
  • 3 β€” CARDINAL: MALE tres, FEMALE tres, NEUTRAL tres; ORDINAL: MALE tercero, FEMALE tercera, NEUTRAL tercer

The constants MALE, FEMALE and NEUTRAL and their spanish counterparts MASCULINO, FEMENINO and NEUTRO, may be imported with the tag ":constants" on module use. periodt ✨

πŸ™ˆ SEXO

Deprecated option only for backward compatibility, use GENERO instead.

bestie pls, she's deprecated. use GENERO. we're not doing SEXO in 2024 πŸ’€

1️⃣ UNMIL

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.

βž– NEGATIVO

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 "menos cinco".

βž• POSITIVO

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 "cinco".

🎁 FORMATO

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.

  • ACCENTS β€” alias for ACENTOS
  • UPPERCASE β€” alias for MAYUSCULAS
  • SEPARATORS β€” alias for SEPARADORES
  • GENDER β€” alias for GENERO
  • POSITIVE β€” alias for POSITIVO
  • NEGATIVE β€” alias for NEGATIVO
  • FORMAT β€” alias for FORMATO

πŸ”’ cardinal

SYNOPSIS:

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

Parameters

$num : the number.

Description

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

This method ignores any fraction part of the number ($num). she simply does not see it, it's not her business πŸ™ˆ

Return Value

Textual representation of the number as a string.


πŸ“ real

SYNOPSIS:

$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 GENDER option, and $genm will default to the $genf's value.

This translation is affected by the options: DECIMAL, SEPARADORES, GENDER, ACENTOS, MAYUSCULAS, POSITIVO, NEGATIVO and FORMATO.

πŸŽ€ Fraction format (FORMATO)

FORMAT 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:

%Ns : Formats the fractional part as text with precisiΓ³n of N digits, for example: number '124.345' formatted with string 'CON %s.' will yield the text 'ciento veinticuatro CON trescientas cuarenta y cinco milΓ©simas', and formatted with string 'CON %2s.' will yield 'ciento veinticuatro CON treinta y cuatro centΓ©simas'.

%Nd : 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' formatted with 'CON %2d ctms.' will yield: 'ciento veinticuatro CON 04 ctms.'


πŸ’œ ordinal

SYNOPSIS:

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

Parameters

$num : the number.

Description

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

This method croacks if $num <= 0 and carps if $num has a fractional part. so like. be normal about your input or she will absolutely let you know πŸ’€

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 πŸ”§

okay bestie, so the functions in this section are generally not used, but they're documented here for completeness. fr fr

⚠️ this is not part of the module's API and it is subject to change. so like… look but don't touch, iykyk πŸ’…

πŸ‘‘ CARDINAL SUPPORT πŸ‘‘

Construction of cardinal numbers

cardinal_e2

SYNOPSIS

cardinal_e2($n, $nn)

PARAMETERS

  • $n β€” the number.
  • $nn β€” word stack.

DESCRIPTION

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

SYNOPSIS

cardinal_e3($n, $nn)

PARAMETERS

  • $n β€” the number.
  • $nn β€” word stack.

DESCRIPTION

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

SYNOPSIS

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

PARAMETERS

  • $n β€” the number.
  • $nn β€” word stack.
  • $mag β€” magnitude of the number: 1 for millions, 2 for billions, etc.
  • $un_mil β€” if true, 1000 is translated as "un mil", otherwise "mil".
  • $postfix β€” array representing plural & singular magnitude of the number, in this order.

DESCRIPTION

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

SYNOPSIS

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

PARAMETERS

  • $n β€” the number.
  • $exp β€” exponent.
  • $fmag β€” closure to format the 6 digits groups.
  • $gen β€” gender of the magnitude (optional, defaults to NEUTRAL):
    • FEMALE for female gender (1 -> una).
    • MALE for male gender (1 -> uno).
    • NEUTRAL for neutral gender (1 -> un).

DESCRIPTION

this function translates the natural number $n to spanish words, adding gender where needed. 🌸

RETURN VALUE

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

cardinal_simple

SYNOPSIS

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

PARAMETERS

  • $n β€” the number.
  • $exp β€” exponent.
  • $un_mil β€” if true, 1000 is translated as "un mil", otherwise "mil".
  • $gen β€” gender of the magnitude (optional, defaults to NEUTRAL):
    • FEMALE for female gender (1 -> una).
    • MALE for male gender (1 -> uno).
    • NEUTRAL for neutral gender (1 -> un).

DESCRIPTION

this function translates 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. 😌

RETURN VALUE

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

fraccion_mag_prefix

SYNOPSIS

fraccion_mag_prefix($mag, $gp)

PARAMETERS

  • $n β€” the number.
  • $exp β€” exponent.
  • $mag β€” magnitude of the number: 1 for millionths, 2 for billionths, etc.
  • $gp β€” gender and plural of the number; it's the concatenation of gender and plural. gender must be one of FEMALE, MALE or NEUTRAL, and plural must be '' for singular and 's' for plural.
    • note that NEUTRAL + plural is a nonsense. πŸ’€
  • $ngen β€” gender of the number (same values as $gen).

DESCRIPTION

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 "milesimos" if $gp is (MALE . 's'). πŸŽ€

RETURN VALUE

Translation of $n to spanish text as a string.

fraccion_simple

SYNOPSIS

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

PARAMETERS

  • $n β€” the number.
  • $exp β€” exponent.
  • $un_mil β€” if true, 1000 is translated as "un mil", otherwise "mil".
  • $gen β€” gender of the magnitude (optional, defaults to NEUTRAL):
    • FEMALE for female gender (1 -> primera).
    • MALE for male gender (1 -> primero).
    • NEUTRAL for neutral gender (1 -> primer).
  • $ngen β€” gender of the number (same values as $gen).

DESCRIPTION

this function translates 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. πŸ’œ

RETURN VALUE

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


πŸ”’ ORDINAL SUPPORT πŸ”’

Construction of ordinal numbers

ordinal_e2

SYNOPSIS

ordinal_e2($n, $nn)

PARAMETERS

  • $n β€” the number.
  • $nn β€” word stack.

DESCRIPTION

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

SYNOPSIS

ordinal_e3($n, $nn)

Parameters

  • $n β€” the number.
  • $nn β€” word stack.

DESCRIPTION

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

SYNOPSIS

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

PARAMETERS

  • $n β€” the number.
  • $nn β€” word stack.
  • $mag β€” magnitude of the number: 1 for millions, 2 for billions, etc.

DESCRIPTION

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

SYNOPSIS

ordinal_simple($n, $exp; $gen)

PARAMETERS

  • $n β€” the number.
  • $exp β€” exponent.
  • $un_mil β€” if true, 1000 is translated as "un mil", otherwise "mil".
  • $gen β€” gender of the magnitude (optional, defaults to NEUTRAL):
    • FEMALE for female gender (1 -> primera).
    • MALE for male gender (1 -> primero).
    • NEUTRAL for neutral gender (1 -> primer).

DESCRIPTION

this function translates 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. πŸ¦‹

RETURN VALUE

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


🧚 MISCELANEOUS 🧚

Everything not fitting elsewhere

parse_num

SYNOPSIS

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', '.', '",');

PARAMETERS

  • $num β€” the number to decompose.
  • $dec β€” decimal separator (typically ',' or '.').
  • $sep β€” separator characters ignored by the parser, usually to mark thousands, millions, etc.

RETURN VALUE

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

  • $sgn β€” sign of the number: -1 if negative, 1 otherwise.
  • $int β€” integer part of the number.
  • $frc β€” decimal (fraction) part of the number.
  • $exp β€” exponent of the number.
🚨 Croaks if there is a syntax error. like, it will literally just give up on you. 😭

retval

SYNOPSIS

$obj->retval($value)

DESCRIPTION

utility method to adjust return values, transforms text following the options: ACENTOS, MAYUSCULAS y HTML.

Returns the adjusted $value. πŸ’…


πŸ“¦ DEPENDENCIES πŸ“¦

Perl 5.006, Exporter, Carp


πŸ‘€ SEE ALSO πŸ‘€

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


πŸ‘‘ AUTHOR πŸ‘‘

Jose Rey, <jrey@cpan.org>


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.


and that's a wrap bestie, no cap 🫢 this whole thing is the perl v5.36.0 build from 2022-11-21, so like, that's the vintage of the Lingua...Numeros(3pm) energy you're getting β€” go forth and count in spanish, you absolute icon πŸŒΈπŸ’–βœ¨

πŸ“¦ where this page comes from