Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: A simple rounding question, but can't get it!

by Masem (Monsignor)
on Feb 01, 2002 at 04:01 UTC ( #142592=note: print w/ replies, xml ) Need Help??


in reply to A simple rounding question, but can't get it!

What is probably happening is that as a float, 20.875 is being represented as "20.8749999999...", which, when rounded to 2 decimal places, is being represented as 20.87. (Though, theorhetically, 0.875 should be able to be represented exactly by decimal fractions, but there's still some floating point math).

A general way to round to some place can be done by the following:

my $rounded = int ( $value / ( 10**$round ) + 0.5 ) * ( 10**$round );
where $round is the digit that you want to round to, 0 being the 1's place, 1 the tens, and -1 the tenths place. (and so forth). You'll want to do something with $round = -2 in your case here.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important


Comment on Re: A simple rounding question, but can't get it!
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://142592]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-08-23 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls