summaryrefslogtreecommitdiff
path: root/locale/ru_utf/Num2text
blob: 86dd855c9cad16fa15319586e429f722882bf830 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # Copyright (C) 2005
  4. #
  5. # Author: Dieter Simader
  6. # Web: http://sourceforge.net/projects/ledger-smb/
  7. #
  8. # Contributors: Vladimir Khaimin <vkhai@yandex.ru>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #======================================================================
  23. #
  24. # this is a variation of the Lingua package
  25. # written for check and receipt printing
  26. # it returns a properly formatted text string
  27. # for a number up to 10**12
  28. sub init {
  29. my $self = shift;
  30. %{ $self->{numbername} } =
  31. (0 => 'ноль',
  32. 1 => 'один',
  33. 2 => 'два',
  34. 3 => 'три',
  35. 4 => 'четыре',
  36. 5 => 'пять',
  37. 6 => 'шесть',
  38. 7 => 'семь',
  39. 8 => 'восемь',
  40. 9 => 'девять',
  41. 10 => 'десять',
  42. 11 => 'одинадцать',
  43. 12 => 'двенадцать',
  44. 13 => 'тринадцать',
  45. 14 => 'четырнадцать',
  46. 15 => 'пятнадцать',
  47. 16 => 'шестнадцать',
  48. 17 => 'семнадцать',
  49. 18 => 'восемнадцать',
  50. 19 => 'девятнадцать',
  51. 20 => 'двадцать',
  52. 30 => 'тридцать',
  53. 40 => 'сорок',
  54. 50 => 'пятьдесят',
  55. 60 => 'шестьдесят',
  56. 70 => 'семьдесят',
  57. 80 => 'восемьдесят',
  58. 90 => 'девяносто',
  59. 10**2 => 'сто',
  60. 10**3 => 'тысяча',
  61. 10**6 => 'миллион',
  62. 10**9 => 'миллиард',
  63. 10**12 => 'триллион'
  64. );
  65. }
  66. sub num2text {
  67. my ($self, $amount) = @_;
  68. return $self->{numbername}{0} unless $amount;
  69. my @textnumber = ();
  70. # split amount into chunks of 3
  71. my @num = reverse split //, abs($amount);
  72. my @numblock = ();
  73. my ($i, $appendn);
  74. my @a = ();
  75. while (@num) {
  76. @a = ();
  77. for (1 .. 3) {
  78. push @a, shift @num;
  79. }
  80. push @numblock, join / /, reverse @a;
  81. }
  82. my $belowhundred = !$#numblock;
  83. while (@numblock) {
  84. $i = $#numblock;
  85. @num = split //, $numblock[$i];
  86. $appendn = "";
  87. $numblock[$i] *= 1;
  88. if ($numblock[$i] == 0) {
  89. pop @numblock;
  90. next;
  91. }
  92. if ($numblock[$i] > 99) {
  93. # the one from hundreds
  94. push @textnumber, $self->{numbername}{$num[0]};
  95. # add hundred designation
  96. push @textnumber, $self->{numbername}{10**2};
  97. # reduce numblock
  98. $numblock[$i] -= $num[0] * 100;
  99. }
  100. $appendn = 'en' if ($i == 2);
  101. $appendn = 'n' if ($i > 2);
  102. if ($numblock[$i] > 9) {
  103. # tens
  104. push @textnumber, $self->format_ten($numblock[$i], $belowhundred);
  105. } elsif ($numblock[$i] > 1) {
  106. # ones
  107. push @textnumber, $self->{numbername}{$numblock[$i]};
  108. } elsif ($numblock[$i] == 1) {
  109. if ($i == 0) {
  110. push @textnumber, $self->{numbername}{$numblock[$i]}.'s';
  111. } else {
  112. if ($i >= 2) {
  113. push @textnumber, $self->{numbername}{$numblock[$i]}.'e';
  114. } else {
  115. push @textnumber, $self->{numbername}{$numblock[$i]};
  116. }
  117. }
  118. $appendn = "";
  119. }
  120. # add thousand, million
  121. if ($i) {
  122. $amount = 10**($i * 3);
  123. push @textnumber, $self->{numbername}{$amount}.$appendn;
  124. }
  125. pop @numblock;
  126. }
  127. join '', @textnumber;
  128. }
  129. sub format_ten {
  130. my ($self, $amount, $belowhundred) = @_;
  131. my $textnumber = "";
  132. my @num = split //, $amount;
  133. if ($amount > 20) {
  134. if ($num[1] == 0) {
  135. $textnumber = $self->{numbername}{$amount};
  136. } else {
  137. if ($belowhundred) {
  138. $amount = $num[0] * 10;
  139. $textnumber = $self->{numbername}{$num[1]}.'und'.$self->{numbername}{$amount};
  140. } else {
  141. $amount = $num[0] * 10;
  142. $textnumber = $self->{numbername}{$amount}.$self->{numbername}{$num[1]};
  143. $textnumber .= 's' if ($num[1] == 1);
  144. }
  145. }
  146. } else {
  147. $textnumber = $self->{numbername}{$amount};
  148. }
  149. $textnumber;
  150. }
  151. 1;