Decimals bug??

  • Thread starter Thread starter Emilio Dabdoub
  • Start date Start date
E

Emilio Dabdoub

I'm having a problem doing a simple addition in VB.NET
?84.16+39.57
123.72999999999999

it should be: 123.73

Where is vb.net getting all this decimals?

Any help will be appreciated.

Thanks
 
Emilio,

One of the first things if you not are upgrading is setting in the options
or in the top of your program files.

Option Strict On

You will see than that you cannot do what you write, because you are adding
doubles.

I hope this helps,

Cor
 
Emilio,

I forgot, please don't write about a bug if you are not sure of that, if
everybody start telling that it is bug, than Microsoft will sure not take
notice from that in this newsgroup.

Thanks in advance.

Cor
 
Emilio said:
I'm having a problem doing a simple addition in VB.NET
?84.16+39.57
123.72999999999999

it should be: 123.73

Where is vb.net getting all this decimals?

I've got a better one!

?0.1+0.1+0.1-0.3
0.000000000000000055511151231257827
Any help will be appreciated.

Don't expect exact results if you use floating-point types.
 
Emilio,
In addition to the other comments, check out:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

http://www.yoda.arachsys.com/csharp/decimal.html


| ?84.16+39.57
| 123.72999999999999

84.16 & 39.57 are doubles, so you are using doubles (binary floating point)
for the calculation.

? CDec(84.16) + CDec(39.57)
123.73D

The doubles are converted to Decimals before the addition, so you are using
Decimal (decimal floating point) for the calculation.

Instead of CDec(84.16) you could have written 84.16, where 84.16D is a
Decimal literal, and 84.16 (or 84.16R) is a Double literal, 84.16F happens
to be a Single literal.

Hope this helps
Jay



| I'm having a problem doing a simple addition in VB.NET
| ?84.16+39.57
| 123.72999999999999
|
| it should be: 123.73
|
| Where is vb.net getting all this decimals?
|
| Any help will be appreciated.
|
| Thanks
|
|
 
That is just the nature of using floating point numbers in a binary
machine. It happens with all languages not just VB or C#. To avoid
these types of problems, use the Decimal datatype.
 

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

Back
Top