round_amount: Anzahl Stellen nicht zu hoch werden lassen
[kivitendo-erp.git] / t / form / round_amount.t
1 use strict;
2 use Test::More;
3
4 use lib 't';
5 use Support::TestSetup;
6
7 Support::TestSetup::login();
8
9 my $config = {};
10
11 $config->{numberformat} = '1.000,00';
12
13 # Positive values
14 is($::form->round_amount(1.05, 2), '1.05', '1.05 @ 2');
15 is($::form->round_amount(1.05, 1), '1.1',  '1.05 @ 1');
16 is($::form->round_amount(1.05, 0), '1',    '1.05 @ 0');
17
18 is($::form->round_amount(1.045, 2), '1.05', '1.045 @ 2');
19 is($::form->round_amount(1.045, 1), '1',    '1.045 @ 1');
20 is($::form->round_amount(1.045, 0), '1',    '1.045 @ 0');
21
22 is($::form->round_amount(33.675, 2), '33.68', '33.675 @ 2');
23 is($::form->round_amount(33.675, 1), '33.7',  '33.675 @ 1');
24 is($::form->round_amount(33.675, 0), '34',    '33.675 @ 0');
25
26 is($::form->round_amount(64.475, 2), '64.48', '64.475 @ 2');
27 is($::form->round_amount(64.475, 1), '64.5',  '64.475 @ 1');
28 is($::form->round_amount(64.475, 0), '64',    '64.475 @ 0');
29
30 is($::form->round_amount(64.475499, 5), '64.4755', '64.475499 @ 5');
31 is($::form->round_amount(64.475499, 4), '64.4755', '64.475499 @ 4');
32 is($::form->round_amount(64.475499, 3), '64.475',  '64.475499 @ 3');
33 is($::form->round_amount(64.475499, 2), '64.48',   '64.475499 @ 2');
34 is($::form->round_amount(64.475499, 1), '64.5',    '64.475499 @ 1');
35 is($::form->round_amount(64.475499, 0), '64',      '64.475499 @ 0');
36
37 is($::form->round_amount(64.475999, 5), '64.476', '64.475999 @ 5');
38 is($::form->round_amount(64.475999, 4), '64.476', '64.475999 @ 4');
39 is($::form->round_amount(64.475999, 3), '64.476', '64.475999 @ 3');
40 is($::form->round_amount(64.475999, 2), '64.48',  '64.475999 @ 2');
41 is($::form->round_amount(64.475999, 1), '64.5',   '64.475999 @ 1');
42 is($::form->round_amount(64.475999, 0), '64',     '64.475999 @ 0');
43
44 is($::form->round_amount(44.9 * 0.75, 2), '33.68', '44.9 * 0.75 @ 2');
45 is($::form->round_amount(44.9 * 0.75, 1), '33.7',  '44.9 * 0.75 @ 1');
46 is($::form->round_amount(44.9 * 0.75, 0), '34',    '44.9 * 0.75 @ 0');
47
48 is($::form->round_amount(143.20, 2), '143.2', '143.20 @ 2');
49 is($::form->round_amount(143.20, 1), '143.2', '143.20 @ 1');
50 is($::form->round_amount(143.20, 0), '143',   '143.20 @ 0');
51
52 is($::form->round_amount(149.175, 2), '149.18', '149.175 @ 2');
53 is($::form->round_amount(149.175, 1), '149.2',  '149.175 @ 1');
54 is($::form->round_amount(149.175, 0), '149',    '149.175 @ 0');
55
56 is($::form->round_amount(198.90 * 0.75, 2), '149.18', '198.90 * 0.75 @ 2');
57 is($::form->round_amount(198.90 * 0.75, 1), '149.2',  '198.90 * 0.75 @ 1');
58 is($::form->round_amount(198.90 * 0.75, 0), '149',    '198.90 * 0.75 @ 0');
59
60 is($::form->round_amount(19610.975, 2), '19610.98', '19610.975 @ 2');
61
62 # Negative values
63 is($::form->round_amount(-1.05, 2), '-1.05', '-1.05 @ 2');
64 is($::form->round_amount(-1.05, 1), '-1.1',  '-1.05 @ 1');
65 is($::form->round_amount(-1.05, 0), '-1',    '-1.05 @ 0');
66
67 is($::form->round_amount(-1.045, 2), '-1.05', '-1.045 @ 2');
68 is($::form->round_amount(-1.045, 1), '-1',    '-1.045 @ 1');
69 is($::form->round_amount(-1.045, 0), '-1',    '-1.045 @ 0');
70
71 is($::form->round_amount(-33.675, 2), '-33.68', '33.675 @ 2');
72 is($::form->round_amount(-33.675, 1), '-33.7',  '33.675 @ 1');
73 is($::form->round_amount(-33.675, 0), '-34',    '33.675 @ 0');
74
75 is($::form->round_amount(-44.9 * 0.75, 2), '-33.68', '-44.9 * 0.75 @ 2');
76 is($::form->round_amount(-44.9 * 0.75, 1), '-33.7',  '-44.9 * 0.75 @ 1');
77 is($::form->round_amount(-44.9 * 0.75, 0), '-34',    '-44.9 * 0.75 @ 0');
78
79 is($::form->round_amount(-149.175, 2), '-149.18', '-149.175 @ 2');
80 is($::form->round_amount(-149.175, 1), '-149.2',  '-149.175 @ 1');
81 is($::form->round_amount(-149.175, 0), '-149',    '-149.175 @ 0');
82
83 is($::form->round_amount(-198.90 * 0.75, 2), '-149.18', '-198.90 * 0.75 @ 2');
84 is($::form->round_amount(-198.90 * 0.75, 1), '-149.2',  '-198.90 * 0.75 @ 1');
85 is($::form->round_amount(-198.90 * 0.75, 0), '-149',    '-198.90 * 0.75 @ 0');
86
87 for my $sign (-1, 1) {
88   for ("00000".."09999") {
89     my $str = my $num = (99 * $sign) . $_;
90     $num /= 100;                 # shift decimal
91     $num /= 5; $num /= 3;        # calc a bit around
92     $num *= 5; $num *= 3;        # dumdidum
93
94     $str =~ s/(..)$/.$1/;       # insert dot
95     $str =~ s/0+$//;            # remove trailing 0
96     $str =~ s/\.$//;            # remove trailing .
97
98     is $::form->round_amount($num, 2), $str, "round($num, 2) == $str";
99   }
100 }
101
102 # what about number that might occur scientific notation?  yes we could just
103 # check round_amount(1e-12, 2) and watch it blow up, but where's the fun? lets
104 # check a few Cardano triplets. they are defined by:
105 #
106 # ∛(a + b√c) + ∛(a - b√c) - 1 = 0
107 #
108 # and the following are solutions for a,b,c:
109 # (2,1,5)
110 # (5,2,13)
111 # (8,3,21)
112 #
113 # now calc that, and see what our round makes of the remaining number near zero
114 #
115 for ([2,1,5], [5,2,13], [8,3,21]) {
116   my ($a,$b,$c) = @$_;
117
118   my $result = ($a + $b * sqrt $c)**(1/3) - ($b * sqrt($c) - $a)**(1/3) - 1;
119
120   is $::form->round_amount($result, 2), '0', "$result => 0";
121 }
122
123 # round to any digit we like
124 my $pi = atan2 0, -1;
125 is $::form->round_amount($pi, 0),  '3',             "0 digits of π";
126 is $::form->round_amount($pi, 1),  '3.1',           "1 digit of π";
127 is $::form->round_amount($pi, 2),  '3.14',          "2 digits of π";
128 is $::form->round_amount($pi, 3),  '3.142',         "3 digits of π";
129 is $::form->round_amount($pi, 4),  '3.1416',        "4 digits of π";
130 is $::form->round_amount($pi, 5),  '3.14159',       "5 digits of π";
131 is $::form->round_amount($pi, 6),  '3.141593',      "6 digits of π";
132 is $::form->round_amount($pi, 7),  '3.1415927',     "7 digits of π";
133 is $::form->round_amount($pi, 8),  '3.14159265',    "8 digits of π";
134 is $::form->round_amount($pi, 9),  '3.141592654',   "9 digits of π";
135 is $::form->round_amount($pi, 10), '3.1415926536', "10 digits of π";
136
137 # A LOT of places:
138 is $::form->round_amount(1.2, 200), '1.2', '1.2 @ 200';
139
140 done_testing;
141
142 1;