仅通过将类型从 int 更改为 decimal,完全相同的值如何在数学结果中有所不同
How can exact same value differ in math result only by changing type from int to decimal
我正在做一些项目,需要做一些数学运算:
decimal X = (Value / 881) * (item.Type ? 130: 130 * 2);
参数“值”例如等于 3000。
如果“值”的类型为 int
,则结果为 390..
如果“值”的类型为 decimal
,则结果为 442.67
这怎么可能??
因为十进制值。如果你一步一步地计算你的公式,你会明白这种差异是由于当你使用 decimal
作为类型
时小数点后的值造成的
当您将 3000(整数)除以 881 时:
int Value = 3000
//Output is 3. Output is in integer
decimal X = (Value / 881); //When int is divided by int then result is in int
当你将 3000(十进制)除以 881 时:
decimal Value = 3000
//Output is 3.4052213393870601589103291714. Output is in decimal.
decimal X = (Value / 881); //When decimal is divided by int then result is in decimal
我希望 .net fiddle 能让您更好地理解我的回答
我正在做一些项目,需要做一些数学运算:
decimal X = (Value / 881) * (item.Type ? 130: 130 * 2);
参数“值”例如等于 3000。
如果“值”的类型为 int
,则结果为 390..
如果“值”的类型为 decimal
,则结果为 442.67
这怎么可能??
因为十进制值。如果你一步一步地计算你的公式,你会明白这种差异是由于当你使用 decimal
作为类型
当您将 3000(整数)除以 881 时:
int Value = 3000
//Output is 3. Output is in integer
decimal X = (Value / 881); //When int is divided by int then result is in int
当你将 3000(十进制)除以 881 时:
decimal Value = 3000
//Output is 3.4052213393870601589103291714. Output is in decimal.
decimal X = (Value / 881); //When decimal is divided by int then result is in decimal
我希望 .net fiddle 能让您更好地理解我的回答