在十进制数据类型进行数学运算在C#中? [英] Performing Math operations on decimal datatype in C#?
问题描述
我在想,如果上面的是在所有可能的。例如:
I was wondering if the above was at all possible. For example:
Math.Sqrt(myVariableHere);
当在超载看,它需要一个双参数,所以我不知道是否有另一种方式与十进制数据类型复制此。
When looking at the overload, it requires a double parameter, so I'm not sure if there is another way to replicate this with decimal datatypes.
推荐答案
在大多数情况下涉及小数
(货币等),它不是的有用的拿根;和根不会有像预期的precision什么,你可能会想到小数
有。
In most cases involving a decimal
(currency etc), it isn't useful to take a root; and the root won't have anything like the expected precision that you might expect a decimal
to have. You can of course force it by casting (assuming we aren't dealing with extreme ends of the decimal
range):
decimal root = (decimal)Math.Sqrt((double)myVariableHere);
这迫使你至少承认固有的四舍五入问题。
which forces you to at least acknowledge the inherent rounding issues.
这篇关于在十进制数据类型进行数学运算在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!