|
-
Nov 30th, 2006, 06:54 AM
#1
Thread Starter
Member
[RESOLVED] Data Type Problem
Hi,
When I multiply a variable of integer data type and a variable of single data type, the result isn't always the right one.
For example - 0.02 * 5 isn't 0.1, but 0.099999994.
Why does this happen? I've tried using double as well, but it's the same problem.
-
Nov 30th, 2006, 09:33 AM
#2
Fanatic Member
Re: Data Type Problem
you will want to format it for display. I just do this:
Format(cDbl(variable * variable), "##0.0000")
Warren Ayen
Senior C# Developer
DLS Software Studios ( http://www.dlssoftwarestudios.com/)
I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
Hey! If you like my post, or I solve your issue, please Rate Me!
-
Nov 30th, 2006, 10:04 AM
#3
Re: Data Type Problem
Floating point values are not supposed to be accurate.
If you are looking for accuracy do not ever use them.
We do financial and payroll systems here - we never ever use float.
-
Nov 30th, 2006, 05:00 PM
#4
Re: Data Type Problem
Try using decimal rather than single.
In general, the problem is that a single/double is the number out to a certain precision. Since the number is binary, not all decimal fractions can be accurately represented in the number of bytes allocated to the number. 0.1 is one tenth, which is a concept hard to capture in a binary system. The way around this is to approximate it. The result you got is 0.1 when you round it off, back to several decimal places, so it is correct....unless you use the entire precision of the number.
There was a lengthy explanation of how fractional numbers are represented in a binary system in the Classic VB forum a few years back. It was thorough, detailed, and probably more than anyone wanted to know, but it has disappeared into the mist of time now.
The decimal type takes care of this better, but you should keep in mind that as long as you are using a floating point number, things like this can happen. Formatting is a good way to display things correctly.
My usual boring signature: Nothing
 
-
Dec 3rd, 2006, 08:22 AM
#5
Thread Starter
Member
Re: [RESOLVED] Data Type Problem
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|