在Java中,BigDecimal类包含的值为A * pow(10,B),其中A为2的补码,其非固定位长,B为32位整数。

在C#中,十进制包含pow(–1,s)×c×pow(10,-e)的值,其中符号s为0或1,系数c由0≤c
我想将Java BigDecimal转换为JAVA中的c#Decimal之类的东西。
你能帮助我吗 。

我有这样的事情

class CS_likeDecimal
{

    private int hi;// for 32bit most sinificant bit of c
    private int mid;// for 32bit in the middle
    private int lo;// for 32 bit the last sinificant bit

    .....
    public CS_likeDecimal(BigDecimal data)
    {
                ....

    }

}


实际上,我找到了这个What's the best way to represent System.Decimal in Protocol Buffers?

它是用于发送c#十进制的协议缓冲区,但是在protobuff-net项目中使用它在c#之间发送消息(但是我想要在c#和JAVA之间)

message Decimal {
  optional uint64 lo = 1; // the first 64 bits of the underlying value
  optional uint32 hi = 2; // the last 32 bis of the underlying value
  optional sint32 signScale = 3; // the number of decimal digits, and the sign


}

谢谢,

最佳答案

我在protobuf-net中使用的Decimal主要用于支持可能在管道两端使用的protobuf-net的用法,它支持固定范围。听起来这两种类型的范围在讨论中是不一样的,所以:不能完全兼容。

我建议明确使用替代表示形式。我不知道Java的BigDecimal有什么表示形式-是实用的byte[]版本还是string版本。

如果您确信缩放比例和范围不会有问题,那么应该可以通过一些摆弄在两个布局之间进行微调。

09-28 13:10
查看更多