Performing Math operations on decimal datatype in C#?

前端 未结 5 1106
情深已故
情深已故 2020-11-30 07:27

I was wondering if the above was at all possible. For example:

Math.Sqrt(myVariableHere);

When looking at the overload, it requires a doub

5条回答
  •  攒了一身酷
    2020-11-30 08:28

    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.

提交回复
热议问题