Solved

Reading file value into variables

Posted on 2002-06-11
6
139 Views
Last Modified: 2010-05-02
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
Comment
Question by:quintyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Expert Comment

by:simonbennett
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

by:Arthur_Wood
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

by:
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:quintyn
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

by:quintyn
ID: 7070573
Thanks again for the quick and good answer
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 7070887
No problem

Glad it's sorted.

Simon
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question