Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl converts to int wrong but only with specific number

the following perl code converts a float number to the wrong integer number

use strict;

my $zahl =297607.22000;
$zahl=$zahl * 100;
print "$zahl\n";
my $text=sprintf ("%017d",$zahl);
print $text;

The output of this is :

29760722
00000000029760721

The thing is, you can change the given number to other numbers and it works.

Any idea what is wrong here or does Perl simply do it wrong?

Thanks for your help!

like image 589
PSilencer Avatar asked Dec 13 '22 10:12

PSilencer


1 Answers

This is related to a FAQ (Why am I getting long decimals). $zahl is not rounded properly, it is rounded down to the next lower integer.

like image 187
hexcoder Avatar answered Jan 14 '23 20:01

hexcoder