2012-02-13 2 views
1

Java BigDecimal 클래스는 A * pow (10, B)로 값을 포함합니다. 여기서 A는 비 수정 비트 길이이고 B는 32 비트 정수입니다.변환 BigDecimal Java와 C# 유사 십진수

C에서 Decimal은 기호 s가 0 또는 1 인 pow (-1, s) x c x pow (10, -e)의 값을 포함합니다. 계수 c는 0 ≤ c ≤< pow 96)이고, 스케일 e는 0 ≤ e ≤ 28이다.

그리고 Java BigDecimal을 Java에서 C# Decimal과 같은 것으로 변환하고 싶습니다. 나를 도울 수 있습니까?

는 나는이 What's the best way to represent System.Decimal in Protocol Buffers?을 발견 사실이

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) 
    { 
       .... 

    } 

} 

같은 몇 가지가 있습니다.

그것, 그러나 이것은 C#을 사이에 메시지를 보낼 수있는 protobuff-NET 프로젝트 사용 보내기 C#을 소수점을위한 프로토콜 버퍼는

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 

}

감사합니다 (하지만 난 C# 및 JAVA 사이에 원하는)

답변

2

Decimal 내가 protobuf-net에서 사용하는 것은 주로 고정 범위를 지원하는 파이프의 양쪽 끝에 사용되는 protobuf-net의 사용 가능성을 지원하기위한 것입니다. 토론에서 두 유형의 범위가 동일하지 않은 것처럼 들리므로 강력하게 호환되지 않습니다.

대체 표현을 사용하여 명시 적으로 제안합니다. 실용적인 byte[] 버전 또는 string 버전이 있는지 여부에 관계없이 Java의 BigDecimal에 대해 어떤 표현을 사용할 수 있는지 알 수 없습니다.

규모와 범위가 문제가되지 않는다고 확신하는 경우 두 가지 레이아웃 사이를 약간 비웃음으로써 바꿔서 표현할 수 있습니다.

0

.Net Decimal 변환기에서 /로 BigDecimal을 써야했습니다. 이 참조 사용 : http://msdn.microsoft.com/en-us/library/system.decimal.getbits.aspx 를이 코드를 쓴 수 일 :

public static byte[] BigDecimalToNetDecimal(BigDecimal paramBigDecimal) throws IllegalArgumentException 
{ 
    // .Net Decimal target 
    byte[] result = new byte[16]; 

    // Unscaled absolute value 
    BigInteger unscaledInt = paramBigDecimal.abs().unscaledValue(); 
    int bitLength = unscaledInt.bitLength(); 
    if (bitLength > 96) 
     throw new IllegalArgumentException("BigDecimal too big for .Net Decimal"); 

    // Byte array 
    byte[] unscaledBytes = unscaledInt.toByteArray(); 
    int unscaledFirst = 0; 
    if (unscaledBytes[0] == 0) 
     unscaledFirst = 1; 

    // Scale 
    int scale = paramBigDecimal.scale(); 
    if (scale > 28) 
     throw new IllegalArgumentException("BigDecimal scale exceeds .Net Decimal limit of 28"); 
    result[1] = (byte)scale; 

    // Copy unscaled value to bytes 8-15 
    for (int pSource = unscaledBytes.length - 1, pTarget = 15; (pSource >= unscaledFirst) && (pTarget >= 4); pSource--, pTarget--) 
    { 
     result[pTarget] = unscaledBytes[pSource]; 
    } 

    // Signum at byte 0 
    if (paramBigDecimal.signum() < 0) 
     result[0] = -128; 

    return result; 
} 

public static BigDecimal NetDecimalToBigDecimal(byte[] paramNetDecimal) 
{ 
    int scale = paramNetDecimal[1]; 
    int signum = paramNetDecimal[0] >= 0 ? 1 : -1; 
    byte[] magnitude = new byte[12]; 
    for (int ptr = 0; ptr < 12; ptr++) magnitude[ptr] = paramNetDecimal[ptr + 4]; 
    BigInteger unscaledInt = new BigInteger(signum, magnitude); 

    return new BigDecimal(unscaledInt, scale); 
}