summaryrefslogtreecommitdiff
path: root/locale/py/Num2text
blob: 88ffe737b9ad5f6dabc195daa3e2a132a50f9e7b (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
  10. # Contributors: John Christian Stoddart
  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. '11o' => 'once',
  42. 12 => 'doce',
  43. 13 => 'trece',
  44. 14 => 'catorce',
  45. 15 => 'quince',
  46. 16 => 'dieciséis',
  47. 17 => 'diecisiete',
  48. 18 => 'dieciocho',
  49. 19 => 'diecinueve',
  50. 20 => 'veinte',
  51. 21 => 'veintiún',
  52. '21o' => 'veintiuno',
  53. 22 => 'veintidós',
  54. 23 => 'veintitrés',
  55. 24 => 'veinticuatro',
  56. 25 => 'veinticinco',
  57. 26 => 'veintiséis',
  58. 27 => 'veintisiete',
  59. 28 => 'veintiocho',
  60. 29 => 'veintinueve',
  61. 30 => 'treinta',
  62. 40 => 'cuarenta',
  63. 50 => 'cincuenta',
  64. 60 => 'sesenta',
  65. 70 => 'setenta',
  66. 80 => 'ochenta',
  67. 90 => 'noventa',
  68. 500 => 'quinientos',
  69. 700 => 'setecientos',
  70. 900 => 'novecientos',
  71. 10**2 => 'ciento',
  72. 10**3 => 'mil',
  73. 10**6 => 'millón',
  74. 10**12 => 'billón',
  75. );
  76. }
  77. sub num2text {
  78. my ($self, $amount) = @_;
  79. return $self->{numbername}{0} unless $amount;
  80. my @textnumber = ();
  81. # split amount into chunks of 3
  82. my @num = reverse split //, abs($amount);
  83. my @numblock = ();
  84. my $stripun = 0;
  85. my @a = ();
  86. my $i;
  87. while (@num) {
  88. @a = ();
  89. for (1 .. 3) {
  90. push @a, shift @num;
  91. }
  92. push @numblock, join / /, reverse @a;
  93. }
  94. # special case for 1000
  95. if ($numblock[1] eq '1' && $numblock[0] gt '000') {
  96. # remove first array element from textnumber
  97. $stripun = 1;
  98. }
  99. while (@numblock) {
  100. $i = $#numblock;
  101. @num = split //, $numblock[$i];
  102. $numblock[$i] *= 1;
  103. if ($numblock[$i] == 0) {
  104. pop @numblock;
  105. next;
  106. }
  107. if ($numblock[$i] > 99) {
  108. if ($num[0] == 1) {
  109. push @textnumber, $self->{numbername}{10**2};
  110. } else {
  111. # special case for 500, 700, 900
  112. if (grep /$num[0]/, (5,7,9)) {
  113. push @textnumber, $self->{numbername}{"${num[0]}00"};
  114. } else {
  115. # the one from hundreds, append cientos
  116. push @textnumber, $self->{numbername}{$num[0]}.$self->{numbername}{10**2}.'s';
  117. }
  118. }
  119. # reduce numblock
  120. $numblock[$i] -= $num[0] * 100;
  121. }
  122. if ($numblock[$i] > 9) {
  123. # tens
  124. push @textnumber, $self->format_ten($numblock[$i], $i);
  125. } elsif ($numblock[$i] > 0) {
  126. # ones
  127. $num = $numblock[$i];
  128. $num .= 'o' if ($num == 1 && $i == 0);
  129. push @textnumber, $self->{numbername}{$num};
  130. }
  131. # add thousand, million
  132. if ($i) {
  133. $num = 10**($i * 3);
  134. if ($numblock[$i] > 1) {
  135. if ($i == 2 || $i == 4) {
  136. $a = $self->{numbername}{$num}."es";
  137. $a =~ s/ó/o/;
  138. push @textnumber, $a;
  139. } elsif ($i == 3) {
  140. $num = 10**($i * 2);
  141. $a = "$self->{10**3} $self->{numbername}{$num}"."es";
  142. $a =~ s/ó/o/;
  143. push @textnumber, $a;
  144. } else {
  145. if ($i == 1) {
  146. push @textnumber, $self->{numbername}{$num};
  147. } else {
  148. push @textnumber, $self->{numbername}{$num}.'s';
  149. }
  150. }
  151. } else {
  152. push @textnumber, $self->{numbername}{$num};
  153. }
  154. }
  155. pop @numblock;
  156. }
  157. shift @textnumber if $stripun;
  158. join ' ', @textnumber;
  159. }
  160. sub format_ten {
  161. my ($self, $amount, $i) = @_;
  162. my $textnumber = "";
  163. my @num = split //, $amount;
  164. if ($amount > 30) {
  165. $textnumber = $self->{numbername}{$num[0]*10};
  166. $amount = $num[1];
  167. } else {
  168. $amount .= 'o' if ($num[1] == 1 && $i == 0);
  169. $textnumber = $self->{numbername}{$amount};
  170. $amount = 0;
  171. }
  172. $textnumber .= " y ".$self->{numbername}{$amount} if $amount;
  173. $textnumber;
  174. }
  175. 1;