473,395 Members | 1,689 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,395 software developers and data experts.

Sum with decimal types

Actually I have two quantities (4.61) stored as decimal numbers.
Why if I try to sum them the result is 9.23 if the correct value is 9.22?

Thank you
Nov 17 '05 #1
7 15306
Are they stored as Decimal or as Single or Double?

When I use this code:

Dim s1, s2 As Decimal

s1 = 4.61D
s2 = 4.61D

MsgBox("Sum: " & (s1 + s2).ToString)

I get 9.22 as expected

Nov 17 '05 #2
Because if you use + there an implicit convertion where it rounds to the
nearest integer value. If you want to sum 2 decimals you should use the static
method "Add" which will undestand that you are dealing with decimal types.

System.Decimal d = System.Decimal.Add(4.61m, 4.61m);
result is 9.22

HTH,

Erick Sgarbi
www.blog.csharpbox.com
Actually I have two quantities (4.61) stored as decimal numbers. Why
if I try to sum them the result is 9.23 if the correct value is 9.22?

Thank you

Nov 17 '05 #3
Alberto <al*****@nospam.com> wrote:
Actually I have two quantities (4.61) stored as decimal numbers.
Why if I try to sum them the result is 9.23 if the correct value is 9.22?


Do you mean actual decimals, or are they doubles? If they're doubles,
you should see
http://www.pobox.com/~skeet/csharp/floatingpoint.html

Otherwise, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
My first thought would be that both values are actually somewhere between
4.61 and 4.6149999. It may be that the numbers originated as doubles or
double constants and were explicitly converted to decimal someplace in your
code.

Be sure to use the "M" or "m" suffix for literal values being assigned to
decimal variables, e.g., 4.61M or 4.61m. Although supposedly there are no
implicit conversions in C# to the decimal type, 4.61 by itself will I
believe be interpreted as a double and then assigned to the decimal
variable, which may constitute an "explicit" conversion. I haven't checked,
but it wouldn't surprise me.

If you view the variables in the debugger, what numbers do you see in there?

--Bob

"Alberto" <al*****@nospam.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Actually I have two quantities (4.61) stored as decimal numbers.
Why if I try to sum them the result is 9.23 if the correct value is 9.22?

Thank you

Nov 17 '05 #5
no***********@csharpbox.com wrote:
Because if you use + there an implicit convertion where it rounds to the
nearest integer value. Are you sure?
When I try

decimal d1 = 4.61m;
decimal d2 = 4.61m;
decimal d = d1 + d2;

it works as expected.
Of course if you were to try and add int and decimal it would but
decimal and decimal?

Cheers
JB
If you want to sum 2 decimals you should use the
static method "Add" which will undestand that you are dealing with
decimal types.

System.Decimal d = System.Decimal.Add(4.61m, 4.61m);
result is 9.22

HTH,

Erick Sgarbi
www.blog.csharpbox.com
Actually I have two quantities (4.61) stored as decimal numbers. Why
if I try to sum them the result is 9.23 if the correct value is 9.22?

Thank you


Nov 17 '05 #6
<no***********@csharpbox.com> wrote:
Because if you use + there an implicit convertion where it rounds to the
nearest integer value.


No there isn't - not if either side is actually a decimal. If it were
rounding to the nearest *integer* value, the result wouldn't be 9.23,
would it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
> My first thought would be that both values are actually somewhere
between 4.61 and 4.6149999. It may be that the numbers originated as
doubles or double constants and were explicitly converted to decimal
someplace in your code
My thoughts exactly...
HTH,

Erick Sgarbi
www.blog.csharpbox.com
My first thought would be that both values are actually somewhere
between 4.61 and 4.6149999. It may be that the numbers originated as
doubles or double constants and were explicitly converted to decimal
someplace in your code.

Be sure to use the "M" or "m" suffix for literal values being assigned
to decimal variables, e.g., 4.61M or 4.61m. Although supposedly there
are no implicit conversions in C# to the decimal type, 4.61 by itself
will I believe be interpreted as a double and then assigned to the
decimal variable, which may constitute an "explicit" conversion. I
haven't checked, but it wouldn't surprise me.

If you view the variables in the debugger, what numbers do you see in
there?

--Bob

"Alberto" <al*****@nospam.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Actually I have two quantities (4.61) stored as decimal numbers. Why
if I try to sum them the result is 9.23 if the correct value is 9.22?

Thank you

Nov 17 '05 #8

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

Similar topics

7
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
by: sushant.bhatia | last post by:
Whats the difference between :- 1. Convert.ToDecimal(stringNumber) 2. Decimal.Parse(stringNumber) 3. decimal.Parse(stringNumber) Thanks. Sushant Bhatia
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
4
by: Ryan Liu | last post by:
Is there a good reason that Decimal does not have Ceiling() method, only Math class has? Thanks!
4
by: illegal.prime | last post by:
Hi all, I have a class that I would like to use to hide the data type I'm using to store decimal point numbers. Failing all else, I will just use object and do conversion/casts as necessary. But...
3
by: bearophileHUGS | last post by:
For me Python is useful to write code that gives *correct* results, allowing me to write it in a short & simple way, with quick debugging cycles (and for other purposes, like to write dynamic code,...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
8
by: Jesper Lund Stocholm | last post by:
I am currently working on converting an old .Net 1.1-application to .Net 3.5. Due to the lack of nullable datatypes in those days, a "decimal wrapper" was created that had various methods to parse...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.