Skip to content

Instantly share code, notes, and snippets.

@PeteGabriel
Created April 15, 2024 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteGabriel/e419b60182c8dd3d0c1588f677729033 to your computer and use it in GitHub Desktop.
Save PeteGabriel/e419b60182c8dd3d0c1588f677729033 to your computer and use it in GitHub Desktop.
decode the Avro logical type Decimal
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