Re: Sum with decimal types

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



notifications@xxxxxxxxxxxxx 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



.



Relevant Pages

  • Re: Algorithm
    ... Wont sum of all positive numbers will be the largest sub-array? ... int getint ... struct sofar *next; ... struct sofar *discard(struct sofar *trail) ...
    (comp.lang.c)
  • Re: Arrays vs Buffers
    ... and the next array access is dependent on the value ... and then return the overall sum to the test harness ... nextPointer(int[] array, int point) ... pointer = nextPointer; ...
    (comp.lang.java.programmer)
  • Re: sum(2,4,3,5,-1,...)
    ... The only example I have to imitate is the one on p.289 ... of an array related to the first argument of the function sum I'm ... int sum ... no args, and get 0, like you can do in Scheme or C++. ...
    (comp.lang.c.moderated)
  • C program to look for large values of | zeta( 1/2 + it) | w.r.t. t
    ... the sum of the terms passed #20,000 in the RS formula. ... int i, j, k; ... unsigned long Nbill; ... int excess; ...
    (sci.math)
  • Re: perfect number
    ... A number is perfect if the sum of its factors (not including ... int is_perfect ... my test driver identified the following perfect ... At least five higher perfect ...
    (comp.programming)