Created
April 15, 2024 09:33
-
-
Save PeteGabriel/e419b60182c8dd3d0c1588f677729033 to your computer and use it in GitHub Desktop.
decode the Avro logical type Decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func decodeAvroDecimal(data []byte) float64 { | |
number := new(big.Int).SetBytes(data) | |
// Check if the number is negative | |
mostSignificantBit := byte(0x80) | |
if data[0]&mostSignificantBit > 0 { | |
// For negative numbers, take two's complement | |
number.Sub(number, new(big.Int).Lsh(big.NewInt(1), uint(len(data))*8)) | |
} | |
decimalRep := decimal.NewFromInt(number.Int64()) | |
divisor := decimal.NewFromInt(baseOfDecimal).Pow(decimal.NewFromInt(scale)) | |
decimalRep = decimalRep.Div(divisor) | |
fVal, _ := decimalRep.Float64() | |
return fVal | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment