问题描述
在Java中,BigDecimal类包含的值为A * pow(10,B),其中A是2的补码,其非固定位长,而B是32位整数.
In Java BigDecimal class contains values as A*pow(10,B) where A is 2's complement which non-fix bit length and B is 32bit integer.
在C#中,小数包含pow(–1,s)×c×pow(10,-e)的值,其中符号s为0或1,系数c由0≤c< pow(2,96),并且标度e使得0≤e≤28.
In C# Decimal contains values as pow (–1,s) × c × pow(10,-e) where the sign s is 0 or 1, the coefficient c is given by 0 ≤ c < pow(2,96) , and the scale e is such that 0 ≤ e ≤ 28 .
我想将Java BigDecimal转换为JAVA中的c#Decimal之类的东西.你能帮我吗?
And i want to convert Java BigDecimal to Something like c# Decimal in JAVA.Can you help me .
我有这样的东西
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)
{
....
}
}
实际上我发现了这个这是什么协议缓冲区中表示System.Decimal的最佳方法?.
它是一个用于发送c#小数的协议缓冲区,但在protobuff-net项目中使用它在c#之间发送消息(但我想在c#和JAVA之间发送消息)
it a protocol buffer for send c# decimal ,but in the protobuff-net project use this to send message between c# (but i want between c# and 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的用法.固定范围.听起来这两种类型的讨论范围不一样,所以:不能完全兼容.
the Decimal
I use in protobuf-net is primarily intended to support the likely usage of protobuf-net being used at both ends of the pipe, which supports a fixed range. It sounds like the range of the two types in discussion is not the same, so: are not robustly compatible.
我建议明确使用替代表示形式.我不知道Java的BigDecimal
有什么表示形式-是实用的byte[]
版本还是string
版本.
I would suggest explicitly using an alternative representation. I don't know what representations are available to Java's BigDecimal
- whether there is a pragmatic byte[]
version, or a string
version.
如果您确信比例尺和范围不会成为问题,那么应该可以在两个布局之间稍作改动.
If you are confident that the scale and range won't be a problem, then it should be possible to fudge between the two layouts with some bit-fiddling.
这篇关于将BigDecimal Java转换为类似c#的Decimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!