[Newbies] How to round a float?

Ben Coman btc at openInWorld.com
Wed Feb 4 14:57:51 UTC 2015


On Wed, Feb 4, 2015 at 7:22 PM, Chuck Hipschman <ckhipschman at gmail.com>
wrote:

> 7 / 8.0 roundTo 0.01 = 0.88  Aha! Thanks!
>
> But:
>
> 100 * (1.05 raisedTo: 10) roundTo: 0.01 162.89000000000001
>
> "162.88946267774418" "unrounded"
> 100 * (1.05 raisedTo: 15) roundTo: 0.01 207.89000000000001
>
>  "207.8928179411367"  "unrounded"
>  Bug, or me again :-)
>
>
If you debug into #roundTo: you'll see its implementation is simple
arithmetic, subject to the vagaries of float resolution.

Float(Number)>>roundTo:
    ^(self / quantum) rounded * quantum

Now are you wanting something nicely formatted for display, or are you
rounding for some mathematic purpose?  Perhaps instead you are wanting...

   100 * (1.05 raisedTo: 10) printShowingDecimalPlaces: 2  " --> 162.89 "

   0.105 printShowingDecimalPlaces: 2    " --> 0.10 "

   0.115 printShowingDecimalPlaces: 2    " --> 0.12 "

btw, I only found these today, looking in the printing protocol of Number
and Float.

cheers -ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20150204/92e05be3/attachment.htm


More information about the Beginners mailing list