在 C# 中对十进制数据类型执行数学运算? [英] Performing Math operations on decimal datatype in C#?

查看:28
本文介绍了在 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.

推荐答案

在大多数涉及decimal(货币等)的情况下,取一个有用根;并且根不会具有您可能期望 decimal 具有的预期精度.你当然可以通过强制转换来强制它(假设我们没有处理 decimal 范围的极端):

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屋!

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