C:\usr\local\share\PassThru\perl>perl -le "print int(100*'1.13')"
112
C:\usr\local\share\PassThru\perl>perl -MPOSIX=round -le "print round(100*'1.13')"
113
####
#### redo, with proper rounding
$from_api = get_from_api();
print "string from_api = '$from_api' straight from api\n";
$from_api .= '.' unless $from_api =~ /\./;
$from_api .= '00';
$from_api =~ s,^(\d+)\.(\d{2})(\d*).*$,${1}${2}.${3},;
print "string from_api = '$from_api' after text manipulation\n";
$from_api = round($from_api);
print "good rounding = ", $from_api, "cents\n";
####
use warnings;
use strict;
use POSIX qw/round/;
use Test::More;
for my $correct_integer_cents ( 0 .. 200 ) {
# first, let's create a testbench using the integer number of cents as the basis
my $price_string = "00" . $correct_integer_cents;
$price_string =~ s/(\d{2})$/.$1/;
my $price_float = $correct_integer_cents / 100;
my $test_name = sprintf "cents=%s str='%s' float='%.15f'", $correct_integer_cents, $price_string, $price_float;
# Now let's compare how your converter and my converter deal with these
is anotherguest($price_string), $correct_integer_cents, "anotherguest | $test_name";
is pryrt($price_string), $correct_integer_cents, "pryrt | $test_name";
}
# so both are identical for those 201 tests.
# however, what about weird situations
for my $price_string ( '1.13', '1.130000000', 1.13 , sprintf('%.18f', 1.13) ) {
is anotherguest($price_string), 113, "anotherguest | str='$price_string'";
is pryrt($price_string), 113, "pryrt | str='$price_string'";
}
done_testing;