convert (long mantissa) and (sbyte exponent) to decimal

半城伤御伤魂 提交于 2019-12-01 05:05:50

问题


upd placed my version in the description at the end

I need to convert mantissa and exponent to decimal. This is how I coded that:

// long field.Decimal.Mantissa
// sbyte field.Decimal.Exponent
decimal MDEntryPx = field.Decimal.Mantissa * (decimal)(Math.Pow(10, field.Decimal.Exponent));

field.Decimal.Mantissa is integer but Math.Pow(10, field.Decimal.Exponent) is double so I afraid that I can lost precision when casting to decimal.

Should I write my own Pow function for integer types that will produce decimal?

What would you suggest? I care about perfomance as I call this functions dozen of thousands times per second! So ugly but fast solutions are highly desired!

And I care about precision as I'm working with money here.

This is what I've just coded, but probably someone can suggest something better:

class Util
{
    private static readonly decimal[] posPow10 = {
                                           1M,
                                           10M,
                                           100M,
                                           1000M,
                                           10000M,
                                           100000M,
                                           1000000M,
                                           10000000M,
                                           100000000M,
                                           1000000000M,
                                           10000000000M,
                                           100000000000M
                                       };

    private static readonly decimal[] negPow10 = {
                                           1M,
                                           0.1M,
                                           0.01M,
                                           0.001M,
                                           0.0001M,
                                           0.00001M,
                                           0.000001M,
                                           0.0000001M,
                                           0.00000001M,
                                           0.000000001M,
                                           0.0000000001M,
                                           0.00000000001M,
                                       };


    public static decimal Pow(decimal mantissa, sbyte exponent)
    {
        decimal result = mantissa;
        if (exponent >= 0)
        {
            result *= posPow10[exponent];
        } else {
            result *= negPow10[-exponent];
        }
        return result;
    }
}

回答1:


There is a constructor of decimal that does pretty much what you want. But it uses the internal representation of decimal, which is different from your representation. The conversion should be pretty simple for numbers with negative exponent, since you can directly translate that into the scale parameter of the constructor. But with positive exponent, I think you will need to do the multiplication by yourself.

Although I have no idea whether it would be actually faster than your code. And it would be certainly much less readable, so your solution might be fine.



来源:https://stackoverflow.com/questions/9993417/convert-long-mantissa-and-sbyte-exponent-to-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!