G
Guest
I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:
timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)
I execute the following line of code:
percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);
and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.
If I replace the code with:
percentOfTotal = 0.00M;
if (totalAmount != 0.00M)
{
percentOfTotal = tkAmount / totalAmount;
}
I get the correct answer. The error does not occur for every transaction as
many of the calcuations come out correct. This is the sixth transaction in a
batch of eight and the only one that is wrong.
The other curious thing is that, using the command window, if I type
? tkAmount / totalAmount
it responds with the correct answer and
? percentOfTransaction
yields the erroneous result indicating that the variables are set properly.
Has anyone seen this behavior before as I use this construct a great deal in
checking for divide by zero?
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:
timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)
I execute the following line of code:
percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);
and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.
If I replace the code with:
percentOfTotal = 0.00M;
if (totalAmount != 0.00M)
{
percentOfTotal = tkAmount / totalAmount;
}
I get the correct answer. The error does not occur for every transaction as
many of the calcuations come out correct. This is the sixth transaction in a
batch of eight and the only one that is wrong.
The other curious thing is that, using the command window, if I type
? tkAmount / totalAmount
it responds with the correct answer and
? percentOfTransaction
yields the erroneous result indicating that the variables are set properly.
Has anyone seen this behavior before as I use this construct a great deal in
checking for divide by zero?