c# - 将 BigDecimal Java 转换为类似 C# 的 Decimal

标签 c# java protocol-buffers bigdecimal

在 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/

相关文章:

c# - 使用泛型和扩展方法将 DataTable 转换为 Dictionary<T1,T2>

c# - 停止 Xamarin.Forms 中的 IOS 弹跳

size - 我可以在 protobuf 中为数字设置最大值吗?

protocol-buffers - 如何使用 protobuf 二进制文件过滤 PUB/SUB?

json - google.protobuf.json_format.MessageToJson 更改字段名称。如何避免?

c# - 如何在 Javascript 中读取页面历史记录?

c# - 在多个文件中搜索多个字符串的 Grep 工具

java - 在 HTTPServlet 请求中编码 UTF-8

jar 外的 Java IO

c# - 是否可以实例化提供其余实现的抽象类?