转换(长尾数)和(为sbyte指数)为十进制 [英] convert (long mantissa) and (sbyte exponent) to decimal

查看:168
本文介绍了转换(长尾数)和(为sbyte指数)为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPD 放在我的版本在描述末



我需要转换尾数和指数为十进制。
这是我如何编码的:

  //长field.Decimal.Mantissa 
//为sbyte field.Decimal.Exponent
十进制MDEntryPx = field.Decimal.Mantissa *(十进制)(Math.Pow(10,field.Decimal.Exponent));



field.Decimal.Mantissa 是整数
,而 Math.Pow(10,field.Decimal.Exponent)是双层,所以我怕转换为十进制的时候,我可以失去精度。



我应该写,将产生整数类型我自己的战俘功能小数



你会怎么建议?我在乎的性能比较,因为我把这个功能每秒几千次打!这么难看,但快速的解决方案非常需要!



和我关心的精度,我在这里钱而工作。



这是我刚才编码,但可能有人可以建议更好的东西:

 类的Util 
{
私人静态只读小数[] posPow10 = {
1M,
10M,
100M,
1000M,
10000M,
100000M,
1000000M,
10000000M,
100000000M,
1000000000M,
10000000000M,
100000000000M
};

私人静态只读小数[] = negPow10 {
1M,
0.1M,
0.01M,
0.001M,
0.0001M ,
0.00001M,
0.000001M,
0.0000001M,
0.00000001M,
0.000000001M,
0.0000000001M,
0.00000000001M,
};


公共静态小数战俘(小数尾数,为sbyte指数)
{
十进制结果=尾数;
如果(指数> = 0)
{
结果* = posPow10 [指数]
}其他{
结果* = negPow10 [-exponent]
}
返回结果;
}
}


解决方案

小数构造 ,做几乎你想要什么。但它使用小数,这是从你的表现不同的内部表示。转换应该是负号指数很简单,因为你可以直接翻译成的在比例参数构造函数。但随着积极的指数,我想你会需要自己做乘法。



虽然我不知道是否会实际上比你的代码更快。这将是肯定要少得多可读性,让您的解决方案可能会被罚款。


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;
    }
}

解决方案

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.

这篇关于转换(长尾数)和(为sbyte指数)为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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