[Newbies] Re: How to round a float?

Chris Cunningham cunningham.cb at gmail.com
Mon Feb 23 19:50:44 UTC 2015


On Fri, Feb 13, 2015 at 4:56 PM, nicolas cellier <
nicolas.cellier.aka.nice at gmail.com> wrote:

> ...
>
> If it's for monetary things like computing interests, then the advice is to
> not use Float but ScaledDecimal.
>
> 100 * (1.05s2 raisedTo: 10) roundTo: 0.01s2
> -> 162.89s2
>
> Nicolas
>
> So, for money, I would suggest not using ScaledDecimal, but rather
FixedDecimal.

ScaledDecimal can do funny things that you would not expect in real money
land:

m := 1/3 asScaledDecimal: 2.
m  "==> 0.33s2"
m + m + m "==> 1.00s2"

You shouldn't be allowed to add 33 cents together and get a whole dollar
(usd, at least)!

FixedDecimal actually rounds to the specified length (ScaledDecimal keeps
the entire precision 'behind the curtains').

m := 1/3 asFixedDecimal: 2.
m  "==> 0.33"
m + m + m "==>  0.99"

http://www.squeaksource.com/FixedDecimal.html

-cbc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20150223/cc37d445/attachment.htm


More information about the Beginners mailing list