summaryrefslogtreecommitdiff
path: root/LedgerSMB/Num2text.pm
blob: d8ecdef34257244c3e4e82be47af1a4adad23950 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # this is the default code for the Check package
  13. #
  14. #=====================================================================
  15. sub init {
  16. my $self = shift;
  17. %{ $self->{numbername} } =
  18. (0 => 'Zero',
  19. 1 => 'One',
  20. 2 => 'Two',
  21. 3 => 'Three',
  22. 4 => 'Four',
  23. 5 => 'Five',
  24. 6 => 'Six',
  25. 7 => 'Seven',
  26. 8 => 'Eight',
  27. 9 => 'Nine',
  28. 10 => 'Ten',
  29. 11 => 'Eleven',
  30. 12 => 'Twelve',
  31. 13 => 'Thirteen',
  32. 14 => 'Fourteen',
  33. 15 => 'Fifteen',
  34. 16 => 'Sixteen',
  35. 17 => 'Seventeen',
  36. 18 => 'Eighteen',
  37. 19 => 'Nineteen',
  38. 20 => 'Twenty',
  39. 30 => 'Thirty',
  40. 40 => 'Forty',
  41. 50 => 'Fifty',
  42. 60 => 'Sixty',
  43. 70 => 'Seventy',
  44. 80 => 'Eighty',
  45. 90 => 'Ninety',
  46. 10**2 => 'Hundred',
  47. 10**3 => 'Thousand',
  48. 10**6 => 'Million',
  49. 10**9 => 'Billion',
  50. 10**12 => 'Trillion',
  51. );
  52. }
  53. sub num2text {
  54. my ($self, $amount) = @_;
  55. return $self->{numbername}{0} unless $amount;
  56. my @textnumber = ();
  57. # split amount into chunks of 3
  58. my @num = reverse split //, abs($amount);
  59. my @numblock = ();
  60. my @a;
  61. my $i;
  62. while (@num) {
  63. @a = ();
  64. for (1 .. 3) {
  65. push @a, shift @num;
  66. }
  67. push @numblock, join / /, reverse @a;
  68. }
  69. while (@numblock) {
  70. $i = $#numblock;
  71. @num = split //, $numblock[$i];
  72. if ($numblock[$i] == 0) {
  73. pop @numblock;
  74. next;
  75. }
  76. if ($numblock[$i] > 99) {
  77. # the one from hundreds
  78. push @textnumber, $self->{numbername}{$num[0]};
  79. # add hundred designation
  80. push @textnumber, $self->{numbername}{10**2};
  81. # reduce numblock
  82. $numblock[$i] -= $num[0] * 100;
  83. }
  84. $numblock[$i] *= 1;
  85. if ($numblock[$i] > 9) {
  86. # tens
  87. push @textnumber, $self->format_ten($numblock[$i]);
  88. } elsif ($numblock[$i] > 0) {
  89. # ones
  90. push @textnumber, $self->{numbername}{$numblock[$i]};
  91. }
  92. # add thousand, million
  93. if ($i) {
  94. $num = 10**($i * 3);
  95. push @textnumber, $self->{numbername}{$num};
  96. }
  97. pop @numblock;
  98. }
  99. join ' ', @textnumber;
  100. }
  101. sub format_ten {
  102. my ($self, $amount) = @_;
  103. my $textnumber = "";
  104. my @num = split //, $amount;
  105. if ($amount > 20) {
  106. $textnumber = $self->{numbername}{$num[0]*10};
  107. $amount = $num[1];
  108. } else {
  109. $textnumber = $self->{numbername}{$amount};
  110. $amount = 0;
  111. }
  112. $textnumber .= " ".$self->{numbername}{$amount} if $amount;
  113. $textnumber;
  114. }
  115. 1;