PDA

View Full Version : may round a float number to the smallest interger bigger than float with using only


lse123
10-14-2008, 09:33 AM
may round a float number to the smallest interger bigger than float with using only

printf "%.2f", $x; // or %d

not using ceil() or else . I must add one to result with %d ? what if I can not use if...else... to choose in case not need add one ?

bazz
10-14-2008, 12:12 PM
look up the sprintf function for the specific code like this


my $rounded = sprintf("%.2f", $price);


That rounds to two decimal places. I am guessing when I say that %.0f is what you need.

bazz


Just read the perl doc and got this which may be of help.


printf '<%.4g>', 100.01; # prints "<100>"


http://perldoc.perl.org/functions/sprintf.html

lse123
10-14-2008, 02:00 PM
I want round up what ever float number is ? i guest %.2f rounds down ? (for positive numbers)

bazz
10-14-2008, 02:23 PM
1st google result for 'perl round up'

http://www.perlmonks.org/?node_id=66948

bazz