sub flt2bit5 {

my $n = shift;

my $res;

for( my $i=5-int($n+0.5); $i>0; $i-- ) {

$res .= "0";

}

for( $i=0; $i<int($n+0.5); $i++ ) {

$res .= "1";

}

return $res;

}

int() is basically a floor() function, so by adding 0.5 you get an actual round(). You have to fill with 0 for 5-n digits and then print n times 1.