[VPM] changing a real to an integer
darren at DarrenDuncan.net
Mon Jan 19 17:36:47 CST 2004
At 2:44 PM -0800 1/19/04, Carl B. Constantine wrote:
>If I have a real nubmer like: -0.0800000000017462 and I want to multiply
>by 1000 and then cut the excess fraction off the end so that all I'm
>left with is -80 instead of -80.0000000017462, how would I do that?
This is what I would do:
$intnum = int(1000*$realnum);
I think the parens for 'int' are optional. Essentially that casts its operand as an int and cuts off any fractional part. I'm not sure if this is a truncation or a round, but I suspect truncation is more likely.
Note that, if you want to round a number to an arbitrary number of places, N, you can do it like this:
$rounded = int($original*(10**N))/(10**N);
If you do it often, this would be good to put in a named function, like round_to_n_places.
-- Darren Duncan
More information about the Victoria-pm