“?:”中的十进制除法错误操作者 [英] Error in decimal divide in "?:" operator

查看:60
本文介绍了“?:”中的十进制除法错误操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#Visual Studio 2005编写的Web服务,用于计算事务中两个或多个计时器之间的拆分

。为了设置它,我有一个特定计时器的几个十进制数据类型变量:


timerRow [" diff_time"] = 4.5M

tkRate = 130M

tkAmount = 585M(timerRow [" diff_time"] * tkRate)

totalAmount = 1605M(交易中所有tkAmounts的总和)


我执行以下代码行:


percentOfTransaction =(totalAmount == 0.00M?0.00M:tkAmount /

totalAmount);


然后得到0.4382022471910112359550561798然而实际答案应该是

0.36448598130841121495327102803738。


如果我用以下代码替换代码:


percentOfTotal = 0.00M;


if(totalAmount!= 0.00M)

{

percentOfTotal = tkAmount / totalAmount;

}


我得到了正确答案。每次交易都不会发生错误,因为很多计算都是正确的。这是

批次中的第六个交易,也是唯一一个错误的交易。


另一个奇怪的事情是,使用命令窗口,如果我输入


? tkAmount / totalAmount


它会回答正确的答案和


? percentOfTransaction


产生错误的结果,表明变量设置正确。


之前有人见过这种行为,因为我使用这个构造很棒交易

检查除以零?


-

问候,


JayAchTee

解决方案

我要做的第一件事就是放入一些括号,看看是否有运营商

分组是我认为的。


" JayAchTee" < Ja ******* @ discussion.microsoft.com写信息

新闻:BD ********************* ************* @ microsof t.com ...


>我有一个用C#Visual Studio 2005编写的Web服务计算
在交易中两个或多个计时员之间拆分
。为了设置它,我有一个特定计时器的几个十进制数据类型变量:


timerRow [" diff_time"] = 4.5M

tkRate = 130M

tkAmount = 585M(timerRow [" diff_time"] * tkRate)

totalAmount = 1605M(交易中所有tkAmounts的总和)


我执行以下代码行:


percentOfTransaction =(totalAmount == 0.00M?0.00M:tkAmount /

totalAmount);


然后得到0.4382022471910112359550561798然而实际答案应该是

0.36448598130841121495327102803738。


如果我用以下代码替换代码:


percentOfTotal = 0.00M;


if(totalAmount!= 0.00M)

{

percentOfTotal = tkAmount / totalAmount;

}


我得到了正确答案。每次交易都不会发生错误

as

许多计算都是正确的。这是第8次交易

,每次8美元,而且唯一一次是错误的。


另一个奇怪的事情就是这样,使用命令窗口,如果我键入


? tkAmount / totalAmount


它会回答正确的答案和


? percentOfTransaction


产生错误的结果,表明变量已正确设置




有没有人见过这种行为之前因为我使用这个构造了很多



检查除以零?


-

问候,


JayAchTee



JayAchTee写道:
< blockquote class =post_quotes>
我有一个用C#Visual Studio 2005编写的Web服务,用于计算事务中两个或多个计时器之间的拆分

。为了设置这个,我有一个来自一个特定计时器的几个十进制数据类型变量:



使用你的代码(大致),我得到:


~


cat prog.cs

使用系统;


class App

{

static void Main()

{

decimal diff_time = 4.5M;

十进制速率= 130M;

十进制数量= diff_time * rate;

十进制totalAmount = 1605M;

十进制百分比= (totalAmount == 0M?0M:金额/

totalAmount);

Console.WriteLine(百分比);

}

}


~


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?

--
Regards,

JayAchTee

解决方案

The first thing I''d do is put in some parentheses and see if the operator
grouping is what I think it is.

"JayAchTee" <Ja*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...

>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?

--
Regards,

JayAchTee



JayAchTee wrote:

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:

Using your code (roughly), I get:

~


cat prog.cs
using System;

class App
{
static void Main()
{
decimal diff_time = 4.5M;
decimal rate = 130M;
decimal amount = diff_time * rate;
decimal totalAmount = 1605M;
decimal percent = (totalAmount == 0M ? 0M : amount /
totalAmount);
Console.WriteLine(percent);
}
}

~


这篇关于“?:”中的十进制除法错误操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆