5 use Support::TestSetup;
7 Support::TestSetup::login();
11 $config->{numberformat} = '1.000,00';
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');
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');
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');
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');
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');
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');
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');
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');
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');
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');
60 is($::form->round_amount(19610.975, 2), '19610.98', '19610.975 @ 2');
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');
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');
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');
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');
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');
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');
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
94 $str =~ s/(..)$/.$1/; # insert dot
95 $str =~ s/0+$//; # remove trailing 0
96 $str =~ s/\.$//; # remove trailing .
98 is $::form->round_amount($num, 2), $str, "round($num, 2) == $str";
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:
106 # ∛(a + b√c) + ∛(a - b√c) - 1 = 0
108 # and the following are solutions for a,b,c:
113 # now calc that, and see what our round makes of the remaining number near zero
115 for ([2,1,5], [5,2,13], [8,3,21]) {
118 my $result = ($a + $b * sqrt $c)**(1/3) - ($b * sqrt($c) - $a)**(1/3) - 1;
120 is $::form->round_amount($result, 2), '0', "$result => 0";
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 π";
138 is $::form->round_amount(1.2, 200), '1.2', '1.2 @ 200';