Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jun 17, 2024
1 parent 7f46499 commit 815466d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/debezium/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package debezium

import (
"fmt"
"math"
"math/big"

"github.com/artie-labs/transfer/lib/typing/decimal"
)

// EncodeDecimal is used to encode a string representation of a number to `org.apache.kafka.connect.data.Decimal`.
func EncodeDecimal(value string, scale int) ([]byte, error) {
scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
bigFloatValue := new(big.Float)
if _, success := bigFloatValue.SetString(value); !success {
return nil, fmt.Errorf("unable to use '%s' as a floating-point number", value)
}
bigFloatValue.Mul(bigFloatValue, new(big.Float).SetInt(scaledValue))

scaledValue := big.NewFloat(math.Pow(10, float64(scale)))
bigFloatValue.Mul(bigFloatValue, scaledValue)

// Extract the scaled integer value.
bigIntValue, _ := bigFloatValue.Int(nil)
Expand Down

0 comments on commit 815466d

Please sign in to comment.