Gossamer Forum
Home : General : Perl Programming :

Rounding up calculations.

Quote Reply
Rounding up calculations.
I'm wondering what the proper usage is in order to round up a calculation?
IE: 5 * 2.5 = 12.5
But I want to display 13

Thanks

Quote Reply
Re: Rounding up calculations. In reply to
That's a FAQ:

Found in C:\Perl\lib\pod\perlfaq4.pod
Does Perl have a round() function? What about ceil() and floor()? Trig funct
ions?

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

for more info: perldoc -q round

to answer your questions:
$rounded = sprintf("% .Xf", $number);

There shuld be no space between the % and the . but this forum does funny things to that. Replace X with the num of decimal places you want to round to

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Rounding up calculations. In reply to
I am not a perl writer, but a time honoured way of achieving rounding (up or down) is:
INT(((5*2.5*10)+5)/10) Result= 13
INT(((5*2.4*10)+5)/10) Result= 13

For what it's worth!

David Olley
Anglo & Foreign International Limited,
http://www.afil.co.uk
Quote Reply
Re: Rounding up calculations. In reply to
Cool thanks guys, will give it a try.