Solved

Reading file value into variables

Posted on 2002-06-11
6
137 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
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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 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…

756 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