473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

rounding error

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.
Feb 5 '08 #1
2 1296
On Feb 5, 4:59 am, "Steve Lloyd" <steve-remo...@-spam-
stevelloyd.co.ukwrote:
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]
Feb 5 '08 #2
Use a Decimal rather than a Double for your result variable

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

hth

Guy

"Steve Lloyd" wrote:
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.
Feb 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Raoul Watson | last post by:
By any chance, anyone got a rounding routine that does a work around the VB "round" bug? I still find it amazing that a company as large as Microsoft would put out a math package that is so...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
6
by: abcd | last post by:
I am trying to write a rounding function. Rounding to 0.05. e.g. I should get below results 6.125 --6.15 1.699 --1.7 1.1985 --1.20 0.5625 --0.60 Can someone have any sample for...
15
by: Mukesh_Singh_Nick | last post by:
Why does floating point have a rounding error? How to work around it? For example, the following: flaot f = 1234.12345678F; printf("%2f\n", f) //prints 1234.123413 and
5
by: Spoon | last post by:
Hello everyone, I don't understand how the lrint() function works. long lrint(double x); The function returns the nearest long integer to x, consistent with the current rounding mode. It...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.