除法不返回我期望的十进制值 [英] Divide not returning the decimal value I expect
本文介绍了除法不返回我期望的十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
此部门有何问题?
Possible Duplicate:
What's wrong with this division?
如果除2 / 3
,它将返回0.66666666666666667
.取而代之的是,我使用双值类型的0.0
和十进制的0
.
If you divide 2 / 3
, it should return 0.66666666666666667
. Instead, I get 0.0
in double value type and 0
in decimal.
我的目的是除以偶数(例如2 / 3
),并始终将其舍入到1.
My purpose is to divide even (e.g. 2 / 3
) and round to 1 always to the nearest.
有帮助吗?
推荐答案
从声音上看,您正在做整数除法.试试这个:
You're doing integer division, from the sounds of it. Try this:
decimal result = 2.0 / 3.0;
对于所有操作,甚至将其强制为小数:
Or even force it to decimals for all of the operations:
decimal result = 2.0m / 3.0m;
这应该给您带来更像您期望的结果.
This should give you a result more like you expect.
这篇关于除法不返回我期望的十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文