Solved

Reading file value into variables

Posted on 2002-06-11
6
133 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now