Solved

Posted on 2002-06-11
132 Views
I am working on an old bookkeeping program who had to be modified because of the Euro.
I get fault results because some values are not read correctly.

Figures were dimensionned as double and than wroten to the file.

Example:

I read a correct 2 decimals file value:

Leverh.Agoed = 7309,34

When I assign it to a variable

Dim dGoederen as Double
dGoederen = Leverh.Agoed

dGoederen has the value: 7309,33984375

or with

Dim cGoederen as Currency
cGoederen = Leverh.Agoed

cGoederen now has a value of 7309,3398

Why those differences?
When I write 7309,34 to the file, I want the same value back when reading!

0
Question by:quintyn
• 2
• 2
• 2

LVL 6

Expert Comment

ID: 7070097
Hi

The difference is due to decimal precision of each datatype. When writing.reading the values try coercing them to 2dp using the format or round functions

eg.

cGoederen = format(cGoederen,"0.00")

or

cGoederen = round(cGoederen,2)

Good Luck

Simon
0

LVL 44

Expert Comment

ID: 7070268
rather than use Double for the Type of cGoederen , have you tried using Currency (which is a special case of Double)
0

LVL 44

Accepted Solution

Arthur_Wood earned 100 total points
ID: 7070307
sorry, didn't look closely enough at the original question.

The problem is that internal ALL numbers are stored in BINARY form, and MOST decimal fractions DO  NOT have an accurate BINARY representation:

Binary-------Decimal
0.1           0.5  = 1/2
0.01          0.25 = 1/4
0.001         0.125 = 1/8   and so on

But that means that 0.7 cannot be completely represented as a BINARY fraction

Not can 0.34.  The only choices are to use the Format Function, of the Round Function, to specify the number of decimal digits that you want to keep.  However, this is only a problem when values are DISPLAYED, as you want to keep the 4 digits of decimal accuracy (for Currency Values) when doing arithmetic, in your application.

Just control how the values are printed (or displayed) by means of the Format Function :

txtValue.Text = Format\$(MyValue,"\$#,###.00")  will "round" to two decimal digits when the value is displayed in the Text Box.

Arthur Wood
0

Author Comment

ID: 7070553
I think I understand now what the problem is.

Both of you gives more or less the same answer, but Arthur_Wood is more detailed. So I will give him the points.
I am sorry for simonbennett I can't split the points!

Anyway, thanks to both of you!

0

Author Comment

ID: 7070573
Thanks again for the quick and good answer
0

LVL 6

Expert Comment

ID: 7070887
No problem

Simon
0

Featured Post

Suggested Solutions

Problem to delete range 4 55
VBA error replacing data 6 35
String manipulation in Visual Basic 7 45
message box in access 4 34
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…