summaryrefslogtreecommitdiff
path: root/t/02-number-handling.t
blob: 0bc786da665572d9ff8b5af39c8ea7e78f7258d6 (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 .. 5) {
  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 .. 5) {
  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 .. 5) {
  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') {
  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. $value = sprintf("%.2f", $value);
  93. cmp_ok($form->parse_amount(\%myconfig, $expected), '==', $value,
  94. "$expected parsed as $formats[$format][0] - $value");
  95. }
  96. }
  97. foreach my $format (0 .. 5) {
  98. %myconfig = ('numberformat' => $formats[$format][0]);
  99. my $thou = $formats[$format][1];
  100. my $dec = $formats[$format][2];
  101. foreach my $rawValue ('10t000d00', '9t999d99', '333d33',
  102. '7t777t777d77', '-12d34') {
  103. $expected = $rawValue;
  104. $expected =~ s/t/$thou/gx;
  105. $expected =~ s/d/$dec/gx;
  106. my $value = $rawValue;
  107. $value =~ s/t//gx;
  108. $value =~ s/d/\./gx;
  109. $value = sprintf("%.2f", $value);
  110. cmp_ok($form->parse_amount(\%myconfig,
  111. $form->parse_amount(\%myconfig, $expected)),
  112. '==', $value,
  113. "$expected parsed as $formats[$format][0] - $value");
  114. }
  115. }