MiniVend Akopia Services

[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date ][Minivend by thread ]

Re: [mv] Rounding solution



Hello All,

I hope this may help. It's very basic stuff but then again... here goes...
For what it's worth...

If you want to truncate a number, do a conversion to int :

 my $floating = 4.78;
 my $truncated = int($floating); # 4

If you want to round off a positive (or negative) number, just add (or
subtract) 0.5 before you truncate :

 my $rounded = int($floating + 0.5); # 4

Here's a way to round off both positive and negative numbers :

 my $truncated = int($floating < 0 ? $floating - 0.5 : $floating + 0.5);

Kind regards,
Bruno-

Fortune of the day :
The sun was shining on the sea,
Shining with all her might :
She did her very best to make
The billows smooth and bright --
And this was very odd, because it was
The middle of the night.
        -- Lewis Carroll


Search for: Match: Format: Sort by: