rounding error

S

Steve Lloyd

Hi,

Can anyone please explain this rounding error on a windows forms project.
I take an input and need to convert from a percentage to a decimal so i do:
dim result as double = cdbl(textbox.text) / 100

when 9.85 is entered into the text box "result" is calcualted as
0.09849999999999999.

For debugging i changed it to cdbl(textbox.text) / 1000 and also /10. both
of these gave the accurate answers, 0.00985 and 0.985 respectively.

Any ideas?

Thanks for any help

Steve.
 
R

rowe_newsgroups

Hi,

Can anyone please explain this rounding error on a windows forms project.
I take an input and need to convert from a percentage to a decimal so i do:
dim result as double = cdbl(textbox.text) / 100

when 9.85 is entered into the text box "result" is calcualted as
0.09849999999999999.

For debugging i changed it to cdbl(textbox.text) / 1000 and also /10. both
of these gave the accurate answers, 0.00985 and 0.985 respectively.

Any ideas?

Thanks for any help

Steve.

It all has to do with the accuracy of doubles and there isn't too much
you can do about it. You might do some searching for the exact "whys",
this topic has been covered numerous times on most programming
newsgroups.

Thanks,

Seth Rowe [MVP]
 
G

guy

Use a Decimal rather than a Double for your result variable

Dim result As Decimal= cdec(textbox.text) / 100

hth

Guy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top