CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Odd rounding issue.: Edit Log


Mar 25, 2013, 4:38 PM

Views: 779
Odd rounding issue.

I've got the following function, wasn't written by me, just being used in the code. For some reason it rounds 29 and 58 down 1, to 28 and 57, but only if I pass it the hash value, if I pass it 29 directly works fine.

sub extFmt 
my ($v, $dbug) = @_;
my $decimalPlaces =2;
my $tmp;
$v = sprintf "%" . ($decimalPlaces + 1) . "." . ($decimalPlaces + 1) ."d\n", $v;
$v =~ /(-?)(\d*)(\d{$decimalPlaces})$/;
$tmp = "$1$2.$3";
return $tmp;

if I do a print Dumper on the values, the only thing I notice is the 29 is in single quotes, but so are other numbers randomly and they work. Any suggestions? I do know it's sprintf doing it, based on dumping the values, but I'm at a loss as to why.

I think I'm just not going to call this function. I'm not sure why they did in the first place. If someone knows why sprintf is treating 29 like a floating point number and why it rounds down, it would be interesting to know.

(This post was edited by lightspd on Mar 25, 2013, 6:02 PM)

Edit Log:
Post edited by lightspd (Novice) on Mar 25, 2013, 5:38 PM
Post edited by lightspd (Novice) on Mar 25, 2013, 6:02 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives