summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: d75fda3d1a3cb66bc993bd376d73a10a89f9e340 (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. }
  101. foreach my $format (0 .. $#formats) {
  102. %myconfig = ('numberformat' => $formats[$format][0]);
  103. my $thou = $formats[$format][1];
  104. my $dec = $formats[$format][2];
  105. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  106. '7t777t777d77', '-12d34', '(76t543d21)') {
  107. $expected = $rawValue;
  108. $expected =~ s/t/$thou/gx;
  109. $expected =~ s/d/$dec/gx;
  110. my $value = $rawValue;
  111. $value =~ s/t//gx;
  112. $value =~ s/d/\./gx;
  113. if ($value =~ m/^\(/gx) {
  114. $value = Math::BigFloat->new('-'.substr($value, 1, -1));
  115. } else {
  116. $value = Math::BigFloat->new($value);
  117. }
  118. cmp_ok($form->parse_amount(\%myconfig,
  119. $form->parse_amount(\%myconfig, $expected)),
  120. '==', $value,
  121. "$expected parsed as $formats[$format][0] - $value");
  122. }
  123. }