-
December 24th, 2009, 12:58 AM
#1
Incerementing a string "single" value
I'm currently using a string as single value
say:
"
Dim test As String
test = 0
"
and with a command button:
"
test = test + 0.1
"
it seems to work fine on my computer and on some others
when i used single, sometimes it turned the value test to 0.5000001
but if i declare test as a string it all works out fine
can anyone explain to me how this is possible ?
-
December 24th, 2009, 02:38 AM
#2
Re: Incerementing a string "single" value
antlion. I have moved your post to a new thread, as you have originally replied to an eight year old thread
Last edited by HanneSThEGreaT; December 24th, 2009 at 03:09 AM.
-
December 28th, 2009, 09:10 AM
#3
Re: Incerementing a string "single" value
It is not usual to compute numbers with string variables, although it works as soon as you have initialized the variable with temp = 0.
If you'd have left temp empty, temp = temp + 0.1 will produce a type mismatch error.
When you work with the data type Single, due to the method how a computer is processing fractional data in binary digits, some rounding error occurs.
This error does not appear when the data type Double is used.
When you use strings, VBs automatic type conversion takes place. Before a string can be used in a numeric expression it will be converted to a numeric data type and the numeric result will be converted bnack to a string before it is stored again in the string variable.
As it seems VB converts both expressions (temp and 0.1) into Doubles before computing, so as the rounding error of the Singles does not appear.
-
January 2nd, 2010, 08:25 AM
#4
Re: Incerementing a string "single" value
It is called a floating point error. It is inherent in the way the processor handles the numeric data. This also applies to the Double data type. However, the Currency data type will give you a few decimal places, but without any floating point error.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
January 3rd, 2010, 05:22 PM
#5
Re: Incerementing a string "single" value
Originally Posted by antlion
I'm currently using a string as single value
say:
"
Dim test As String
test = 0
"
and with a command button:
"
test = test + 0.1
"
it seems to work fine on my computer and on some others
when i used single, sometimes it turned the value test to 0.5000001
but if i declare test as a string it all works out fine
can anyone explain to me how this is possible ?
Did you tried something like this?
Dim test As String
test = CStr(0)
and with a command button:"
test = CStr( CSng(test) + 0.1 )
I didn't run the code, but it shoud work with explicit type conversion.
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
|