Article View: comp.databases.ms-access
Article #335Re: Rounding problems with Access
From: Shamil Salakhetdinov
Date: Thu, 15 Jul 1999 00:00
Date: Thu, 15 Jul 1999 00:00
103 lines
3344 bytes
3344 bytes
Larry,
Use currency data type for financial calculations, don't mix currency with
single, write your own rounding function to properly round results when you
mix currency with single and double.
This code:
Dim Looper As Currency ' Single
For Looper = 0 To 1 Step 0.1
Debug.Print Looper
Stop
Next Looper
Results in:
0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
1
And this code:
Dim SharesToPurchase As Long
Dim Bank As Currency
Dim ClosingPrice As Currency 'Single
Bank = 1000000
ClosingPrice = 15.06
SharesToPurchase = 66401
SharesToPurchase = Int(Bank / ClosingPrice)
Debug.Print "Bank Balance Before Transaction = " & Str(Bank)
Debug.Print "Closing Price = " & Str(ClosingPrice)
Debug.Print "Shares To Purchase = " & Str(SharesToPurchase)
Debug.Print "Transaction Amount = " & Str(SharesToPurchase *
ClosingPrice)
Bank = Bank - (SharesToPurchase * ClosingPrice)
Debug.Print "Bank Balance After Transaction = " & Str(Bank)
Produces the following results:
Bank Balance Before Transaction = 1000000
Closing Price = 15.06
Shares To Purchase = 66401
Transaction Amount = 999999.06
Bank Balance After Transaction = .94
HTH,
Shamil
P.S. This rounding problem isn't only MS Access problem - this is VBA/VB
engines common problem/feature...
--
Shamil Salakhetdinov
DARTS Ltd., St.Petersburg, Russia
e-mail: shamil@marta.darts.spb.ru
Web: http://members.xoom.com/shamil_s
Larry LeBlanc <larry.leblanc@sympatico.ca> wrote in message
news:wQkj3.4$PZ3.247@198.235.216.4...
> Good day,
>
> Can anyone explain to me why the following source code:
>
> ===================================
> Private Sub Command9_Click()
>
> Dim Looper As Single
>
> For Looper = 0 To 1 Step 0.1
> Debug.Print Looper
> Stop
> Next Looper
>
> End Sub
> ===================================
>
> leads to the following results:
>
> ===================================
> 0
> 0.1
> 0.2
> 0.3
> 0.4
> 0.5
> 0.6
> 0.7
> 0.8000001
> 0.9000001
> ===================================
>
<tail skipped>
Technical Details
Message-ID:
<7mkoui$2t3$1@devil.nevalink.ru>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<wQkj3.4$PZ3.247@198.235.216.4>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111a5a,73518ce6c7069b00 X-Google-Attributes: gid111a5a,public X-Google-Thread: ff0ac,73518ce6c7069b00 X-Google-Attributes: gidff0ac,public X-Google-Thread: 102b1d,73518ce6c7069b00 X-Google-Attributes: gid102b1d,public X-Google-Thread: 1131f0,73518ce6c7069b00 X-Google-Attributes: gid1131f0,public From: "Shamil Salakhetdinov" <shamil@marta.darts.spb.ru> Subject: Re: Rounding problems with Access Date: 1999/07/15 Message-ID: <7mkoui$2t3$1@devil.nevalink.ru> X-Deja-AN: 501326867 Content-Transfer-Encoding: 7bit References: <wQkj3.4$PZ3.247@198.235.216.4> X-Priority: 3 Content-Type: text/plain; charset="koi8-r" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Trace: devil.nevalink.ru 932046610 2979 195.190.105.183 (15 Jul 1999 13:50:10 GMT) Organization: DARTS Ltd. X-MSMail-Priority: Normal Mime-Version: 1.0 NNTP-Posting-Date: 15 Jul 1999 13:50:10 GMT Newsgroups: comp.databases.ms-access,de.comp.datenbanken.ms-access,es.comp.bd.ms-access,microsoft.public.access.modulesdaovba