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