summaryrefslogtreecommitdiff
path: root/locale/it/Num2text
blob: a330a78582e9491169efc1ee932ce1274e587a9a (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # Copyright (C) 2002
  4. #
  5. # Original Author: Dieter Simader
  6. # Email: dsimader@sql-ledger.org
  7. # Web: http://sourceforge.net/projects/sql-ledger/
  8. #
  9. # Contributors: Luca Venturini <luca@yepa.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. #======================================================================
  24. #
  25. # this is a variation of the Lingua package
  26. # written for check and receipt printing
  27. # it returns a properly formatted text string
  28. # for a number up to 10**12
  29. sub init {
  30. my $self = shift;
  31. %{ $self->{numbername} } =
  32. (0 => 'Zero',
  33. 1 => 'uno',
  34. 2 => 'due',
  35. 3 => 'tre',
  36. 4 => 'quattro',
  37. 5 => 'cinque',
  38. 6 => 'sei',
  39. 7 => 'sette',
  40. 8 => 'otto',
  41. 9 => 'nove',
  42. 10 => 'dieci',
  43. 11 => 'undici',
  44. 12 => 'dodici',
  45. 13 => 'tredici',
  46. 14 => 'quattrodici',
  47. 15 => 'quindici',
  48. 16 => 'sedici',
  49. 17 => 'diciassette',
  50. 18 => 'diciotto',
  51. 19 => 'diciannove',
  52. 20 => 'venti',
  53. 30 => 'trenta',
  54. 40 => 'quaranta',
  55. 50 => 'cinquanta',
  56. 60 => 'sessanta',
  57. 70 => 'settanta',
  58. 80 => 'ottanta',
  59. 90 => 'novanta',
  60. 10**2 => 'cento',
  61. 10**3 => 'mille',
  62. 10**6 => 'milione',
  63. 10**9 => 'miliardo',
  64. 10**12 => 'mille miliardi'
  65. );
  66. }
  67. sub num2text {
  68. my ($self, $amount) = @_;
  69. return $self->{numbername}{0} unless $amount;
  70. my @textnumber = ();
  71. # split amount into chunks of 3
  72. my @num = reverse split //, abs($amount);
  73. my @numblock = ();
  74. my ($i, $appendn);
  75. my @a = ();
  76. while (@num) {
  77. @a = ();
  78. for (1 .. 3) {
  79. push @a, shift @num;
  80. }
  81. push @numblock, join / /, reverse @a;
  82. }
  83. while (@numblock) {
  84. $i = $#numblock;
  85. @num = split //, $numblock[$i];
  86. $numblock[$i] *= 1;
  87. if ($numblock[$i] == 0) {
  88. pop @numblock;
  89. next;
  90. }
  91. if ($numblock[$i] > 99) {
  92. # the one from hundreds
  93. push @textnumber, $self->{numbername}{$num[0]};
  94. # add hundred designation
  95. push @textnumber, $self->{numbername}{10**2};
  96. # reduce numblock
  97. $numblock[$i] -= $num[0] * 100;
  98. }
  99. if ($numblock[$i] > 9) {
  100. # tens
  101. push @textnumber, $self->format_ten($numblock[$i]);
  102. } elsif ($numblock[$i] > 1) {
  103. # ones
  104. push @textnumber, $self->{numbername}{$numblock[$i]};
  105. }
  106. # add thousand, million
  107. if ($i) {
  108. $amount = 10**($i * 3);
  109. push @textnumber, $self->{numbername}{$amount};
  110. }
  111. pop @numblock;
  112. }
  113. join '', @textnumber;
  114. }
  115. sub format_ten {
  116. my ($self, $amount) = @_;
  117. my $textnumber = "";
  118. my @num = split //, $amount;
  119. if ($amount > 20) {
  120. if ($num[1] == 0) {
  121. $textnumber = $self->{numbername}{$amount};
  122. } else {
  123. $amount = $num[0] * 10;
  124. $textnumber = $self->{numbername}{$amount}.$self->{numbername}{$num[1]};
  125. }
  126. } else {
  127. $textnumber = $self->{numbername}{$amount};
  128. }
  129. $textnumber;
  130. }
  131. 1;