in reply to Re: Trimming of the decimal part.
in thread Trimming of the decimal part.

It should be noted that int always rounds down, while sprintf will flexibly round the proper direction. I didn't notice which way the OP wanted, nor whether the distinction would matter in the particular case. It's a good issue of which to be aware, though.

Replies are listed 'Best First'.
Re^3: Trimming of the decimal part.
by mscharrer (Hermit) on Apr 23, 2008 at 18:49 UTC
    I know that int( ) is truncating the fractional part away, i.e. is always rounding down. The OP said: But I do not want the decimal part printed in the above output.. This means truncation to me. Maybe I took it to literal.

    So far I can remember my last encounter with true rounding in Perl the sprinf function is really the best, like you already said. For other rounding functions like floor() and ceil() the POSIX module can be used.

    Update:
    Just tested sprintf("%d") and it truncates like int(). A function which implements "normal" rounding, away from zero from .5 starting is:

    sub round ($) { return sprintf("%.0f", shift); }