473,394 Members | 1,773 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.

Decimals bug??

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
Nov 21 '05 #1
5 1011
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
Nov 21 '05 #2
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
Nov 21 '05 #3

Emilio Dabdoub wrote:
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.

--
Larry Lard
Replies to group please

Nov 21 '05 #4
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.

Nov 21 '05 #5
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

"Emilio Dabdoub" <em****@sandluis.com> wrote in message
news:O6**************@TK2MSFTNGP10.phx.gbl...
| 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
|
|
Nov 21 '05 #6

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

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
0
by: thx | last post by:
I have MySQL 4.0.18 on Windows 2000 and I have a problem with floats and decimals. The table has floats and decimals fields but when I enter non-integers in these fields, they get rounded. So if I...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.