Link to home
Start Free TrialLog in
Avatar of rgbcof
rgbcof

asked on

Perl, floor and ceiling a floating number

$floatnum = "1234.5678";
# I want to floor the number to 3rd digit after the decimal.  So 1234.567
# also, ceiling it to:  1234.568
SOLUTION
Avatar of whosbetterthanme
whosbetterthanme
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgbcof
rgbcof

ASKER

$floatnum = "1234.5672";

# the ceiling doesn't work if the last digit is less than 5.
# ceiling should be 1234.568
Avatar of rgbcof

ASKER

Doesn't take care of all case.   I will post a different question.
Too bad I had a solution for you but got busy and you closed it :(
use POSIX;
$floatnum = "1234.5678";
$floor=floor($floatnum*1000)/1000;
$ceiling=ceil($floatnum*1000)/1000;
#note that neither 1234.567 nor 1234.568 can be represented exactly in IEEE floating point