Jeff,
On 23 Sep 2011, at 08:17, Jeff Gray wrote:
> I am reading a list of numbers from a csv file. The file represents financial
> transactions so all the numbers are currency and they have zero, one or 2
> decimal places
> Eg 1000 240.46 100.5
>
> I use Number readFrom: to get from text to number.
>
> When I add all the amounts the answer comes out like: 306.65999999999985
>
> I want to see 306.66
>
> I had alook at Stef's chapter talking aboy 2s compliment and I'm guessing
> that this is something like that but I read most of it as: meow meow meow
> moew. Too hard :-) Can someone just tell me which class/method I need?
> Thanks.
> I know this might come across as lazy but my background is as an application
> developer and I'm just not that interested in the low level system guts. And
> I'm slightly lazy.....
You could try reading them as ScaledDecimals and compute with those, these are supposed to be exact as far as I understand it.
(('100 240.46 100.51' findTokens: ' ') collect: [ :each | ScaledDecimal readFrom: each ])
inject: 0 into: [ :next :sum | next + sum ]
If and when necessary, you can convert to a float using #asFloat.
HTH,
Sven