summaryrefslogtreecommitdiff
path: root/locale/co_utf/Num2text
blob: 03fe16d0f3b8ca9615678d80130716be7389f261 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # Copyright (C) 2002
  4. #
  5. # Author: Dieter Simader
  6. # Email: dsimader@sql-ledger.org
  7. # Web: http://sourceforge.net/projects/ledger-smb/
  8. #
  9. # Language: Spanish UTF-8
  10. # Contributors:
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. #======================================================================
  25. sub init {
  26. my $self = shift;
  27. %{ $self->{numbername} } =
  28. (0 => 'cero',
  29. 1 => 'un',
  30. '1o' => 'uno',
  31. 2 => 'dos',
  32. 3 => 'tres',
  33. 4 => 'cuatro',
  34. 5 => 'cinco',
  35. 6 => 'seis',
  36. 7 => 'siete',
  37. 8 => 'ocho',
  38. 9 => 'nueve',
  39. 10 => 'diez',
  40. 11 => 'once',
  41. 12 => 'doce',
  42. 13 => 'trece',
  43. 14 => 'catorce',
  44. 15 => 'quince',
  45. 16 => 'dieciséis',
  46. 17 => 'diecisiete',
  47. 18 => 'dieciocho',
  48. 19 => 'diecinueve',
  49. 20 => 'veinte',
  50. 21 => 'veintiún',
  51. '21o' => 'veintiuno',
  52. 22 => 'veintidós',
  53. 23 => 'veintitrés',
  54. 24 => 'veinticuatro',
  55. 25 => 'veinticinco',
  56. 26 => 'veintiséis',
  57. 27 => 'veintisiete',
  58. 28 => 'veintiocho',
  59. 29 => 'veintinueve',
  60. 30 => 'treinta',
  61. 40 => 'cuarenta',
  62. 50 => 'cincuenta',
  63. 60 => 'sesenta',
  64. 70 => 'setenta',
  65. 80 => 'ochenta',
  66. 90 => 'noventa',
  67. 500 => 'quinientos',
  68. 700 => 'setecientos',
  69. 900 => 'novecientos',
  70. 10**2 => 'ciento',
  71. 10**3 => 'mil',
  72. 10**6 => 'millón',
  73. 10**12 => 'billón',
  74. );
  75. }
  76. sub num2text {
  77. my ($self, $amount) = @_;
  78. return $self->{numbername}{0} unless $amount;
  79. my @textnumber = ();
  80. # split amount into chunks of 3
  81. my @num = reverse split //, abs($amount);
  82. my @numblock = ();
  83. my $stripun = 0;
  84. my @a = ();
  85. my $i;
  86. while (@num) {
  87. @a = ();
  88. for (1 .. 3) {
  89. push @a, shift @num;
  90. }
  91. push @numblock, join / /, reverse @a;
  92. }
  93. # special case for 1000
  94. if ($numblock[1] eq '1' && $numblock[0] gt '000') {
  95. # remove first array element from textnumber
  96. $stripun = 1;
  97. }
  98. while (@numblock) {
  99. $i = $#numblock;
  100. @num = split //, $numblock[$i];
  101. $numblock[$i] *= 1;
  102. if ($numblock[$i] == 0) {
  103. pop @numblock;
  104. next;
  105. }
  106. if ($numblock[$i] > 99) {
  107. if ($num[0] == 1) {
  108. push @textnumber, $self->{numbername}{10**2};
  109. } else {
  110. # special case for 500, 700, 900
  111. if (grep /$num[0]/, (5,7,9)) {
  112. push @textnumber, $self->{numbername}{"${num[0]}00"};
  113. } else {
  114. # the one from hundreds, append cientos
  115. push @textnumber, $self->{numbername}{$num[0]}.$self->{numbername}{10**2}.'s';
  116. }
  117. }
  118. # reduce numblock
  119. $numblock[$i] -= $num[0] * 100;
  120. }
  121. if ($numblock[$i] > 9) {
  122. # tens
  123. push @textnumber, $self->format_ten($numblock[$i], $i);
  124. } elsif ($numblock[$i] > 0) {
  125. # ones
  126. $num = $numblock[$i];
  127. $num .= 'o' if ($num == 1 && $i == 0);
  128. push @textnumber, $self->{numbername}{$num};
  129. }
  130. # add thousand, million
  131. if ($i) {
  132. $num = 10**($i * 3);
  133. if ($numblock[$i] > 1) {
  134. if ($i == 2 || $i == 4) {
  135. $a = $self->{numbername}{$num}."es";
  136. $a =~ s/ó/o/;
  137. push @textnumber, $a;
  138. } elsif ($i == 3) {
  139. $num = 10**($i * 2);
  140. $a = "$self->{10**3} $self->{numbername}{$num}"."es";
  141. $a =~ s/ó/o/;
  142. push @textnumber, $a;
  143. } else {
  144. if ($i == 1) {
  145. push @textnumber, $self->{numbername}{$num};
  146. } else {
  147. push @textnumber, $self->{numbername}{$num}.'s';
  148. }
  149. }
  150. } else {
  151. push @textnumber, $self->{numbername}{$num};
  152. }
  153. }
  154. pop @numblock;
  155. }
  156. shift @textnumber if $stripun;
  157. join ' ', @textnumber;
  158. }
  159. sub format_ten {
  160. my ($self, $amount, $i) = @_;
  161. my $textnumber = "";
  162. my @num = split //, $amount;
  163. if ($amount > 30) {
  164. $textnumber = $self->{numbername}{$num[0]*10};
  165. $amount = $num[1];
  166. } else {
  167. $amount .= 'o' if ($num[1] == 1 && $i == 0);
  168. $textnumber = $self->{numbername}{$amount};
  169. $amount = 0;
  170. }
  171. $textnumber .= " y ".$self->{numbername}{$amount} if $amount;
  172. $textnumber;
  173. }
  174. 1;