Rounding off decimals to the nearest number in PERL

Hi Guys,

I am generating a statistical report , below is the snippet of the code :

Now, $nSlices stands for the time duration,meaning,the statistics will be displayed for that particular time duration. Trouble is, for certain values of $totalTime (which is the end time - start time ), i end up getting decimal values for $nSlices. I am coding this in PERL, and i wish i could find a way to round off the decimals to the nearest whole number. For example, if the value of $nSlices is 23.323232 i would like to round it off to 23, and if the value of $nSlices is 23.7878787 i would like to round it off to 24. Basically,anything over .5 and above gets rounded off to the next whole number, and anything below .5 gets rounded off to the previous number. Anyways that i can achieve this? Help would be much appreciated!

this might help you:b:

echo "$nSlices"|awk '{printf "%d",$0+0.5}'

Thank you! Will try it later tonight and let you know, i am not quiet sure about the $0+0.5 part though, does that imply we add a 0.5 constant to every value of $nSlices?

From perlfaq

Thanks Rikxik! I guess that's gonna work! I am trying to write a logical if else statement in Perl to use the 'ceil' and 'floor' functions , but my novice programming skills is hampering the progress!

This is what i am trying to accomplish , if the value of a variable $nSlice is a number greater than .5 (ex : 23.5 or 16.8 or 17.9 etc), ceil that number,else floor that number. Any suggestions or help would be appreciated. Thanks again!

More than likely you do not need to use the POSIX module, your numbers do not appear to be that critical or important, using sprintf or printf should work.

Below is the snippet of the code that i have written. The damn sprintf is not working. No matter what number i enter, it is entering ceil and not floor.Any reasons why ?

#!/usr/bin/perl

# SOME VARIABLE

use POSIX;

print "Enter a number = ";

$nSlices1 = <STDIN>;

($bdec, $adec ) = split( /\./, $nSlices1 ); # Split the variable at the decimal point and put it into 2 temporary variables


print " The value of numbers before decimal point is $bdec \n";

sprintf("%.3f", $adec) ;

print " The value of numbers after  decimal point is $adec \n";


if ($adec >= 50)

{
     $ceil = ceil($nSlices1); 
     print "The value of $nSlices1 has been rounded off to $ceil \n";
    } 

else 

{
      $floor = floor($nSlices1);
        
       print "The value of input has been rounded off to $floor  \n";
        
}
#!/usr/bin/perl
use POSIX;

my ($nSlices, $bdec, $adec, $rnd);
print "Enter a number = ";
$nSlices1 = <STDIN>;
($bdec, $adec ) = split( /\./, $nSlices1 ); 
$adec = substr($adec,0,1);

print "The value of numbers: before decimal=$bdec after decimal=$adec ";
$rnd = ($adec >= 5) ? ceil($nSlices1) : floor($nSlices1);
print "Rounded to: $rnd\n";

Output:

$ fc.pl
Enter a number = 5.467
The value of numbers: before decimal=5 after decimal=4 Rounded to: 5
$ fc.pl
Enter a number = 5.647
The value of numbers: before decimal=5 after decimal=6 Rounded to: 6

Thanks rikxik! It works like a treat! :slight_smile:

#!/usr/bin/perl

# SOME VARIABLE

print "Enter a number = ";

$nSlices1 = <STDIN>;
chomp $nSlices1;
print " The value of numbers before decimal point is $nSlices1 \n";

$nSlices1 = sprintf("%d", $nSlices1);

print " The value of numbers after  decimal point is $nSlices1 \n";