NUmber rounding

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

NUmber rounding

Jeff Gray
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.....
 
Reply | Threaded
Open this post in threaded view
|

Re: NUmber rounding

Sven Van Caekenberghe
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


Reply | Threaded
Open this post in threaded view
|

Re: NUmber rounding

Jeff Gray
Thanks Sven.
Works a treat.
Without the asFloat the printOn: displays 306.66s2 (that is, it appends the scale).
The asFloat message then sorts that out and I get just what I need.
Reply | Threaded
Open this post in threaded view
|

Re: NUmber rounding

David T. Lewis
On Fri, Sep 23, 2011 at 07:18:50AM -0700, Jeff Gray wrote:
> Thanks Sven.
> Works a treat.
> Without the asFloat the printOn: displays 306.66s2 (that is, it appends the
> scale).
> The asFloat message then sorts that out and I get just what I need.

Just a caution in case you are not aware - don't use floating point
for financial calculations. A quick google search will give you the
details, but it is essentially *never* ok to do financial calculations
in floating point math. ScaledDecimal should be fine though.

Dave