X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=t%2Fform%2Fround_amount.t;h=18553c0f487ba79bf8cb6527be7f88e4fc5c16f1;hb=5474d397bbc42ffaea0b51ebb4bb9a4c7afaa881;hp=685013a44b76d5274ee0a4c2821854779d385c19;hpb=add0f69be977025f141c7ddb4803f642980a87a0;p=kivitendo-erp.git diff --git a/t/form/round_amount.t b/t/form/round_amount.t index 685013a44..18553c0f4 100644 --- a/t/form/round_amount.t +++ b/t/form/round_amount.t @@ -45,6 +45,10 @@ is($::form->round_amount(44.9 * 0.75, 2), '33.68', '44.9 * 0.75 @ 2'); is($::form->round_amount(44.9 * 0.75, 1), '33.7', '44.9 * 0.75 @ 1'); is($::form->round_amount(44.9 * 0.75, 0), '34', '44.9 * 0.75 @ 0'); +is($::form->round_amount(143.20, 2), '143.2', '143.20 @ 2'); +is($::form->round_amount(143.20, 1), '143.2', '143.20 @ 1'); +is($::form->round_amount(143.20, 0), '143', '143.20 @ 0'); + is($::form->round_amount(149.175, 2), '149.18', '149.175 @ 2'); is($::form->round_amount(149.175, 1), '149.2', '149.175 @ 1'); is($::form->round_amount(149.175, 0), '149', '149.175 @ 0'); @@ -53,6 +57,8 @@ is($::form->round_amount(198.90 * 0.75, 2), '149.18', '198.90 * 0.75 @ 2'); is($::form->round_amount(198.90 * 0.75, 1), '149.2', '198.90 * 0.75 @ 1'); is($::form->round_amount(198.90 * 0.75, 0), '149', '198.90 * 0.75 @ 0'); +is($::form->round_amount(19610.975, 2), '19610.98', '19610.975 @ 2'); + # Negative values is($::form->round_amount(-1.05, 2), '-1.05', '-1.05 @ 2'); is($::form->round_amount(-1.05, 1), '-1.1', '-1.05 @ 1'); @@ -78,6 +84,59 @@ is($::form->round_amount(-198.90 * 0.75, 2), '-149.18', '-198.90 * 0.75 @ 2'); is($::form->round_amount(-198.90 * 0.75, 1), '-149.2', '-198.90 * 0.75 @ 1'); is($::form->round_amount(-198.90 * 0.75, 0), '-149', '-198.90 * 0.75 @ 0'); +for my $sign (-1, 1) { + for ("00000".."09999") { + my $str = my $num = (99 * $sign) . $_; + $num /= 100; # shift decimal + $num /= 5; $num /= 3; # calc a bit around + $num *= 5; $num *= 3; # dumdidum + + $str =~ s/(..)$/.$1/; # insert dot + $str =~ s/0+$//; # remove trailing 0 + $str =~ s/\.$//; # remove trailing . + + is $::form->round_amount($num, 2), $str, "round($num, 2) == $str"; + } +} + +# what about number that might occur scientific notation? yes we could just +# check round_amount(1e-12, 2) and watch it blow up, but where's the fun? lets +# check a few Cardano triplets. they are defined by: +# +# ∛(a + b√c) + ∛(a - b√c) - 1 = 0 +# +# and the following are solutions for a,b,c: +# (2,1,5) +# (5,2,13) +# (8,3,21) +# +# now calc that, and see what our round makes of the remaining number near zero +# +for ([2,1,5], [5,2,13], [8,3,21]) { + my ($a,$b,$c) = @$_; + + my $result = ($a + $b * sqrt $c)**(1/3) - ($b * sqrt($c) - $a)**(1/3) - 1; + + is $::form->round_amount($result, 2), '0', "$result => 0"; +} + +# round to any digit we like +my $pi = atan2 0, -1; +is $::form->round_amount($pi, 0), '3', "0 digits of π"; +is $::form->round_amount($pi, 1), '3.1', "1 digit of π"; +is $::form->round_amount($pi, 2), '3.14', "2 digits of π"; +is $::form->round_amount($pi, 3), '3.142', "3 digits of π"; +is $::form->round_amount($pi, 4), '3.1416', "4 digits of π"; +is $::form->round_amount($pi, 5), '3.14159', "5 digits of π"; +is $::form->round_amount($pi, 6), '3.141593', "6 digits of π"; +is $::form->round_amount($pi, 7), '3.1415927', "7 digits of π"; +is $::form->round_amount($pi, 8), '3.14159265', "8 digits of π"; +is $::form->round_amount($pi, 9), '3.141592654', "9 digits of π"; +is $::form->round_amount($pi, 10), '3.1415926536', "10 digits of π"; + +# A LOT of places: +is $::form->round_amount(1.2, 200), '1.2', '1.2 @ 200'; + done_testing; 1;