在 Java BigDecimal 类中,包含 A*pow(10,B) 形式的值,其中 A 是非固定位长度的 2 的补码,B 是 32 位整数。
在 C# Decimal 中,十进制包含的值为 pow (–1,s) × c × pow(10,-e),其中符号 s 为 0 或 1,系数 c 由下式给出: 0 ≤ c < pow(2,96 ) ,尺度 e 满足 0 ≤ e ≤ 28 。
我想将 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#十进制的 Protocol Buffer ,但在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
版本。
如果您确信比例和范围不会成为问题,那么应该可以通过一些小改动在两种布局之间进行调整。
关于c# - 将 BigDecimal Java 转换为类似 C# 的 Decimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9258313/