summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: a59b0ac5ead331a8f45c80f0adffa11ed78f08ce (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More 'no_plan';
  5. use Math::BigFloat;
  6. use LedgerSMB::Form;
  7. my $form = new Form;
  8. my %myconfig;
  9. ok(defined $form);
  10. isa_ok($form, 'Form');
  11. my $expected;
  12. foreach my $value ('0.01', '0.05', '0.015', '0.025', '1.1', '1.5', '1.9',
  13. '10.01', '4', '5', '5.1', '5.4', '5.5', '5.6', '6', '0',
  14. '0.000', '10.155', '55', '0.001', '14.5', '15.5', '4.5') {
  15. foreach my $places ('3', '2', '1', '0') {
  16. Math::BigFloat->round_mode('+inf');
  17. $expected = Math::BigFloat->new($value)->ffround(-$places);
  18. is($form->round_amount($value, $places), $expected,
  19. "$value to $places decimal places - $expected");
  20. Math::BigFloat->round_mode('-inf');
  21. $expected = Math::BigFloat->new(-$value)->ffround(-$places);
  22. is($form->round_amount(-$value, $places), $expected,
  23. "-$value to $places decimal places - $expected");
  24. }
  25. foreach my $places ('-1', '-2') {
  26. Math::BigFloat->round_mode('+inf');
  27. $expected = Math::BigFloat->new($value)->ffround(-($places-1));
  28. is($form->round_amount($value, $places), $expected,
  29. "$value to $places decimal places - $expected");
  30. Math::BigFloat->round_mode('-inf');
  31. $expected = Math::BigFloat->new(-$value)->ffround(-($places-1));
  32. is($form->round_amount(-$value, $places), $expected,
  33. "-$value to $places decimal places - $expected");
  34. }
  35. }
  36. # TODO Number formatting still needs work for l10n
  37. my @formats = (['1,000.00', ',', '.'], ["1'000.00", "'", '.'],
  38. ['1.000,00', '.', ','], ['1000,00', '', ','],
  39. ['1000.00', '', '.'], ['1 000.00', ' ', '.']);
  40. my %myfooconfig = (numberformat => '1000.00');
  41. foreach my $format (0 .. $#formats) {
  42. %myconfig = (numberformat => $formats[$format][0]);
  43. my $thou = $formats[$format][1];
  44. my $dec = $formats[$format][2];
  45. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  46. '7t777t777d77', '-12d34') {
  47. $expected = $rawValue;
  48. $expected =~ s/t/$thou/gx;
  49. $expected =~ s/d/$dec/gx;
  50. my $value = $rawValue;
  51. $value =~ s/t//gx;
  52. $value =~ s/d/\./gx;
  53. ##$value = Math::BigFloat->new($value);
  54. $value = $form->parse_amount(\%myfooconfig,$value);
  55. is($form->format_amount(\%myconfig, $value, 2, 'x'), $expected,
  56. "$value formatted as $formats[$format][0] - $expected");
  57. }
  58. }
  59. foreach my $format (0 .. $#formats) {
  60. %myconfig = (numberformat => $formats[$format][0]);
  61. my $thou = $formats[$format][1];
  62. my $dec = $formats[$format][2];
  63. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  64. '7t777t777d77', '-12d34') {
  65. $expected = $rawValue;
  66. $expected =~ s/t/$thou/gx;
  67. $expected =~ s/d/$dec/gx;
  68. my $value = $rawValue;
  69. $value =~ s/t//gx;
  70. $value =~ s/d/\./gx;
  71. #my $ovalue = $value;
  72. $value = $form->parse_amount(\%myfooconfig,$value);
  73. #$value = $form->parse_amount(\%myconfig,$value);
  74. is($form->format_amount(\%myconfig,
  75. $form->format_amount(\%myconfig, $value, 2, 'x'),
  76. 2, 'x'),
  77. $expected, "Double formatting of $value as $formats[$format][0] - $expected");
  78. }
  79. }
  80. foreach my $format (0 .. $#formats) {
  81. %myconfig = ('numberformat' => $formats[$format][0]);
  82. my $thou = $formats[$format][1];
  83. my $dec = $formats[$format][2];
  84. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  85. '7t777t777d77', '-12d34', '(76t543d21)') {
  86. $expected = $rawValue;
  87. $expected =~ s/t/$thou/gx;
  88. $expected =~ s/d/$dec/gx;
  89. my $value = $rawValue;
  90. $value =~ s/t//gx;
  91. $value =~ s/d/\./gx;
  92. if ($value =~ m/^\(/gx) {
  93. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  94. } else {
  95. $value = Math::BigFloat->new($value);
  96. }
  97. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  98. "$expected parsed as $formats[$format][0] - $value");
  99. }
  100. $expected = '12 CR';
  101. my $value = Math::BigFloat->new('12');
  102. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  103. "$expected parsed as $formats[$format][0] - $value");
  104. $expected = '21 DR';
  105. $value = Math::BigFloat->new('-21');
  106. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  107. "$expected parsed as $formats[$format][0] - $value");
  108. }
  109. foreach my $format (0 .. $#formats) {
  110. %myconfig = ('numberformat' => $formats[$format][0]);
  111. my $thou = $formats[$format][1];
  112. my $dec = $formats[$format][2];
  113. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  114. '7t777t777d77', '-12d34', '(76t543d21)') {
  115. $expected = $rawValue;
  116. $expected =~ s/t/$thou/gx;
  117. $expected =~ s/d/$dec/gx;
  118. my $value = $rawValue;
  119. $value =~ s/t//gx;
  120. $value =~ s/d/\./gx;
  121. if ($value =~ m/^\(/gx) {
  122. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  123. } else {
  124. $value = Math::BigFloat->new($value);
  125. }
  126. cmp_ok($form->parse_amount(\%myconfig,
  127. $form->parse_amount(\%myconfig, $expected)),
  128. '==', $value,
  129. "$expected parsed as $formats[$format][0] - $value");
  130. }
  131. $expected = '12 CR';
  132. my $value = Math::BigFloat->new('12');
  133. cmp_ok($form->parse_amount(\%myconfig,
  134. $form->parse_amount(\%myconfig, $expected)),
  135. '==', $value,
  136. "$expected parsed as $formats[$format][0] - $value");
  137. $expected = '21 DR';
  138. $value = Math::BigFloat->new('-21');
  139. cmp_ok($form->parse_amount(\%myconfig,
  140. $form->parse_amount(\%myconfig, $expected)),
  141. '==', $value,
  142. "$expected parsed as $formats[$format][0] - $value");
  143. }