summaryrefslogtreecommitdiff
path: root/LedgerSMB/Num2text.pm
blob: 6688ee32afc3aa9e8e75cfecba4a55d34ef7dd80 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://sourceforge.net/projects/ledger-smb/
  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. %{ $self->{numbername} } =
  36. (0 => 'Zero',
  37. 1 => 'One',
  38. 2 => 'Two',
  39. 3 => 'Three',
  40. 4 => 'Four',
  41. 5 => 'Five',
  42. 6 => 'Six',
  43. 7 => 'Seven',
  44. 8 => 'Eight',
  45. 9 => 'Nine',
  46. 10 => 'Ten',
  47. 11 => 'Eleven',
  48. 12 => 'Twelve',
  49. 13 => 'Thirteen',
  50. 14 => 'Fourteen',
  51. 15 => 'Fifteen',
  52. 16 => 'Sixteen',
  53. 17 => 'Seventeen',
  54. 18 => 'Eighteen',
  55. 19 => 'Nineteen',
  56. 20 => 'Twenty',
  57. 30 => 'Thirty',
  58. 40 => 'Forty',
  59. 50 => 'Fifty',
  60. 60 => 'Sixty',
  61. 70 => 'Seventy',
  62. 80 => 'Eighty',
  63. 90 => 'Ninety',
  64. 10**2 => 'Hundred',
  65. 10**3 => 'Thousand',
  66. 10**6 => 'Million',
  67. 10**9 => 'Billion',
  68. 10**12 => 'Trillion',
  69. );
  70. }
  71. sub num2text {
  72. my ($self, $amount) = @_;
  73. return $self->{numbername}{0} unless $amount;
  74. my @textnumber = ();
  75. # split amount into chunks of 3
  76. my @num = reverse split //, abs($amount);
  77. my @numblock = ();
  78. my @a;
  79. my $i;
  80. while (@num) {
  81. @a = ();
  82. for (1 .. 3) {
  83. push @a, shift @num;
  84. }
  85. push @numblock, join / /, reverse @a;
  86. }
  87. while (@numblock) {
  88. $i = $#numblock;
  89. @num = split //, $numblock[$i];
  90. if ($numblock[$i] == 0) {
  91. pop @numblock;
  92. next;
  93. }
  94. if ($numblock[$i] > 99) {
  95. # the one from hundreds
  96. push @textnumber, $self->{numbername}{$num[0]};
  97. # add hundred designation
  98. push @textnumber, $self->{numbername}{10**2};
  99. # reduce numblock
  100. $numblock[$i] -= $num[0] * 100;
  101. }
  102. $numblock[$i] *= 1;
  103. if ($numblock[$i] > 9) {
  104. # tens
  105. push @textnumber, $self->format_ten($numblock[$i]);
  106. } elsif ($numblock[$i] > 0) {
  107. # ones
  108. push @textnumber, $self->{numbername}{$numblock[$i]};
  109. }
  110. # add thousand, million
  111. if ($i) {
  112. $num = 10**($i * 3);
  113. push @textnumber, $self->{numbername}{$num};
  114. }
  115. pop @numblock;
  116. }
  117. join ' ', @textnumber;
  118. }
  119. sub format_ten {
  120. my ($self, $amount) = @_;
  121. my $textnumber = "";
  122. my @num = split //, $amount;
  123. if ($amount > 20) {
  124. $textnumber = $self->{numbername}{$num[0]*10};
  125. $amount = $num[1];
  126. } else {
  127. $textnumber = $self->{numbername}{$amount};
  128. $amount = 0;
  129. }
  130. $textnumber .= " ".$self->{numbername}{$amount} if $amount;
  131. $textnumber;
  132. }
  133. 1;