# [Newbies] Re: How to round a float?

nicolas cellier nicolas.cellier.aka.nice at gmail.com
Sat Feb 14 00:56:02 UTC 2015

```Louis LaBrunda <Lou <at> Keystone-Software.com> writes:

>
> Hi Guys,
>
> On Wed, 4 Feb 2015 08:22:02 -0500, Johann Hibschman <johannh <at> gmail.com>
> wrote:
>
> >That just looks like floating-point representation. It's as rounded as it
> >can get!
> >-Johann
>
> At first I thought some more parans "(" were needed but that didn't change
> anything.  No this sounds like a bug to me (ar at least something that can
> be improved) as VA Smalltalk prints 162.89, so somehow they are able to
> make the floats do the right thing.
>
> Lou
>

Lou, I'm very suspicious about what VA does.
IMO you should not trust too much what you see (what it prints!)
Because 16289*0.01 and 162.89 are not really the same float...

(16289*0.01) asFraction printShowingMaxDecimalPlaces: 1074
'162.890000000000014779288903810083866119384765625'

((162.89) asTrueFraction printShowingMaxDecimalPlaces: 1074)
'162.8899999999999863575794734060764312744140625'

For having least surprising read-eval-print loops here are 3 levels of
expectations:
1) every two different Float shall have a different printString;
2) every Float printString shall be reinterpreted to the same Float;
3) every Float shall be printed to the shortest representation that will be
reinterpreted to the same Float.
Squeak responds to level-3, so somehow does the right thing.

I suspect VA has lower expectations (only print the first n decimals, even
if Float are different, they will print the same...).

The question is why rounding to 0.01?

If it's just to print, then it's better to do so with some instructions like
100 * (1.05 raisedTo: 10) printShowingDecimalPlaces: 2.
-> '162.89'

For versatile printing options, I suggest loading NumberPrinter package from
http://ss3.gemstone.com/ss/NumberPrinter/

(FloatPrinter fixed) digitCount: 2; print: 100 * (1.05s2 raisedTo: 10)
-> '162.89'

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

```

More information about the Beginners mailing list