Solved

Reading file value into variables

Posted on 2002-06-11
6
135 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

785 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