Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to round my output.

My current output:
fileone 345.234 filetwo 89.7
I need output of:
fileone 345 filetwo 89
Please advise how I can do this with below:
my $size = ((stat($file))[7]) / (1000);

Replies are listed 'Best First'.
Re: Rounding numbers output
by grinder (Bishop) on Jan 22, 2003 at 14:26 UTC

    You appear to be dealing with sizes of files. In that regard, you should really be dividing by 1024 to get sensible values.

      my $size = sprintf '%d', ((stat($file))[7]) / 1024;

    That will give you what you want. Oh, I nearly forgot, if you are looking for human-readable sizes, this has been covered in pretty print bytes.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      Thanks
Re: Rounding numbers output
by BrowserUk (Patriarch) on Jan 22, 2003 at 13:48 UTC

    As it appears from your sample that you actually mean 'truncate' rather than 'round', take a look at perlfunc:int.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Rounding numbers output
by Aragorn (Curate) on Jan 22, 2003 at 14:31 UTC
    If you want rounding, you could use sprintf like this:
      print sprintf("%.0f\n", 345.234);
      print sprintf("%.0f\n", 89.7);
    
    This represent reality a bit better, IMHO.

    Arjen