[Newbies] Re: Rouding Floats up to a number of decimals - Method
Finder
nicolas cellier
ncellier at ifrance.com
Wed Jul 23 18:43:15 UTC 2008
cdrick a écrit :
> I couldn't find such a function. I tryed in the method finder but find
> nothing: MethodFinder methodFor: #(#(1.2288 3) 1.228).
>
> Do somebody know if such a method exist ?
>
> Meanwhile I did that:
>
> Float>>trimAfter: aNumberOfDecimal "roundAfter:"
> | number |
> number := 10 raisedTo: aNumberOfDecimal.
> ^(self * 10^aNumber) truncated "rounded" / 10^aNumber asFloat
>
> and still MethodFinder methodFor: #(#(1.2288 3) 1.228) returns '(no
> single method does that function) '
> whereas 1.2288 roundedAfter: 3 returns 1.229
>
> Does anybody know what was wrong in my method finder use ?
>
> Thanks
>
> Cédrick
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
Try with
MethodFinder methodFor: #(#(1001.2288 0.001) 1001.228).
It works even if (1001.2288 truncateTo: 0.001) ~= 1001.228, because of
inexact arithmetic (in fact, it may depends which library is reading the
Float; results are different if above decimal representation are rounded
to nearest Float as SqNumberParser does).
The magic behind is that MethodFinder uses closeTo: rather than =, and
this is right because no one should rely on Float equality after
performing some inexact operations.
Nicolas
More information about the Beginners
mailing list