summaryrefslogtreecommitdiff
path: root/LedgerSMB/Num2text.pm
blob: 252f8f06d64fdcbd14d18408aa927cfe6261c310 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2002
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has NOT undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # this is the default code for the Check package
  31. #
  32. #=====================================================================
  33. sub init {
  34. my $self = shift;
  35. my $locale = $self->{'locale'};
  36. %{ $self->{numbername} } =
  37. (0 => $locale->text('Zero'),
  38. 1 => $locale->text('One'),
  39. 2 => $locale->text('Two'),
  40. 3 => $locale->text('Three'),
  41. 4 => $locale->text('Four'),
  42. 5 => $locale->text('Five'),
  43. 6 => $locale->text('Six'),
  44. 7 => $locale->text('Seven'),
  45. 8 => $locale->text('Eight'),
  46. 9 => $locale->text('Nine'),
  47. 10 => $locale->text('Ten'),
  48. 11 => $locale->text('Eleven'),
  49. 12 => $locale->text('Twelve'),
  50. 13 => $locale->text('Thirteen'),
  51. 14 => $locale->text('Fourteen'),
  52. 15 => $locale->text('Fifteen'),
  53. 16 => $locale->text('Sixteen'),
  54. 17 => $locale->text('Seventeen'),
  55. 18 => $locale->text('Eighteen'),
  56. 19 => $locale->text('Nineteen'),
  57. 20 => $locale->text('Twenty'),
  58. 30 => $locale->text('Thirty'),
  59. 40 => $locale->text('Forty'),
  60. 50 => $locale->text('Fifty'),
  61. 60 => $locale->text('Sixty'),
  62. 70 => $locale->text('Seventy'),
  63. 80 => $locale->text('Eighty'),
  64. 90 => $locale->text('Ninety'),
  65. 10**2 => $locale->text('Hundred'),
  66. 10**3 => $locale->text('Thousand'),
  67. 10**6 => $locale->text('Million'),
  68. 10**9 => $locale->text('Billion'),
  69. 10**12 => $locale->text('Trillion'),
  70. );
  71. }
  72. sub num2text {
  73. my ($self, $amount) = @_;
  74. return $self->{numbername}{0} unless $amount;
  75. my @textnumber = ();
  76. # split amount into chunks of 3
  77. my @num = reverse split //, abs($amount);
  78. my @numblock = ();
  79. my @a;
  80. my $i;
  81. while (@num) {
  82. @a = ();
  83. for (1 .. 3) {
  84. push @a, shift @num;
  85. }
  86. push @numblock, join / /, reverse @a;
  87. }
  88. while (@numblock) {
  89. $i = $#numblock;
  90. @num = split //, $numblock[$i];
  91. if ($numblock[$i] == 0) {
  92. pop @numblock;
  93. next;
  94. }
  95. if ($numblock[$i] > 99) {
  96. # the one from hundreds
  97. push @textnumber, $self->{numbername}{$num[0]};
  98. # add hundred designation
  99. push @textnumber, $self->{numbername}{10**2};
  100. # reduce numblock
  101. $numblock[$i] -= $num[0] * 100;
  102. }
  103. $numblock[$i] *= 1;
  104. if ($numblock[$i] > 9) {
  105. # tens
  106. push @textnumber, $self->format_ten($numblock[$i]);
  107. } elsif ($numblock[$i] > 0) {
  108. # ones
  109. push @textnumber, $self->{numbername}{$numblock[$i]};
  110. }
  111. # add thousand, million
  112. if ($i) {
  113. $num = 10**($i * 3);
  114. push @textnumber, $self->{numbername}{$num};
  115. }
  116. pop @numblock;
  117. }
  118. join ' ', @textnumber;
  119. }
  120. sub format_ten {
  121. my ($self, $amount) = @_;
  122. my $textnumber = "";
  123. my @num = split //, $amount;
  124. if ($amount > 20) {
  125. $textnumber = $self->{numbername}{$num[0]*10};
  126. $amount = $num[1];
  127. } else {
  128. $textnumber = $self->{numbername}{$amount};
  129. $amount = 0;
  130. }
  131. $textnumber .= " ".$self->{numbername}{$amount} if $amount;
  132. $textnumber;
  133. }
  134. 1;