Re: Need help rounding off my dollar amount
From: René Allan Larsen (rene.larsen_at_spamfilter.dk)
Date: 11/06/03
- Previous message: eshipman: "Re: TBorderStyle and bsSingle"
- In reply to: Pat Mc: "Need help rounding off my dollar amount"
- Next in thread: Pat Mc: "Re: Need help rounding off my dollar amount"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 06 Nov 2003 19:45:20 +0100
In article <qktjqv0md2ddv58d4hqq3dl53887shqmjs@4ax.com>, Pat Mc wrote:
>
> Basically, I want a function to round off my dollar value. Sounded
> simple enough when I first started it !!
>
> Examples would be
> 1.22 goes to 1.20
> 1.23 goes to 1.25
> 1.98 goes to 2.00
>
> In my initial attempt, I passed a currency amount to my function and
> had ideas of isolating the cents, tens of cents and dollar values. It
> became messy in the type conversions between currency, string and
> floating - I had string amounts of 123.2450 instead of nice 123.25.
>
> Is there a standard Currency 'Rounding' function 'out there' where I
> can pass a value to it and it returns a value to 2 decimal places? or
> can someone offer advice or some rules to go by.
The algorithm is simple:
Divide value by 0.05, as you want to round to nearest 0.05
Add 0.5 to value
Truncate decimals
Multiply value by 0.05
Here is a worked example:
> 1.22 goes to 1.20
Divide: 1.22 / 0.05 -> 24.40
Add: 24.40 + 0.5 -> 24.90
Trunc: 24.90 -> 24
Multiply: 24 * 0.05 -> 1.20
> 1.23 goes to 1.25
Divide: 1.23 / 0.05 -> 24.60
Add: 24.60 + 0.5 -> 25.10
Trunc: 25.10 -> 25
Multiply: 25 * 0.05 -> 1.25
Now this may *not* give you a correct answer because of the fact, that
floating point calculations are imprecise on a PC.
Try looking for this post in the archives:
From: "John Herbster \(TeamB\)" <herb-sci at novus-tele.net>
Newsgroups: borland.public.attachments
Subject: T_Currency_1 -- test of dollar and cents rounding.
Date: Thu, 28 Nov 2002 09:51:11 -0800
And this one too:
From: "John Herbster" <johnh@petronworld.com>
Newsgroups: borland.public.attachments
Subject: Unit for doing best possible decimal rounding.
Date: Tue, 30 Apr 2002 13:44:13 -0500
Regards, René
- Previous message: eshipman: "Re: TBorderStyle and bsSingle"
- In reply to: Pat Mc: "Need help rounding off my dollar amount"
- Next in thread: Pat Mc: "Re: Need help rounding off my dollar amount"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|