experiments with pgtype numeric

This commit is contained in:
2024-12-16 23:37:34 +01:00
parent 8b7b7883f1
commit c5e848c941
4 changed files with 157 additions and 5 deletions
+67 -5
View File
@@ -2,19 +2,21 @@ package pgutils
import (
"log/slog"
"math"
"math/big"
"strconv"
"github.com/jackc/pgx/v5/pgtype"
)
func NumericToFloat64(n pgtype.Numeric) float64 {
value, err := n.Value()
val, err := n.Value()
if err != nil {
slog.Error("error getting numeric value", "error", err)
return 0
}
strValue, ok := value.(string)
strValue, ok := val.(string)
if !ok {
slog.Error("error converting numeric value to string")
return 0
@@ -29,10 +31,70 @@ func NumericToFloat64(n pgtype.Numeric) float64 {
return floatValue
}
func FloatToNumeric(number float64) (value pgtype.Numeric) {
parse := strconv.FormatFloat(number, 'f', -1, 64)
func NumericToInt64(n pgtype.Numeric) int64 {
return n.Int.Int64() * int64(math.Pow(10, float64(n.Exp)))
}
func FloatToNumeric(number float64, precision int) (value pgtype.Numeric) {
parse := strconv.FormatFloat(number, 'f', precision, 64)
slog.Info("parse", "parse", parse)
if err := value.Scan(parse); err != nil {
slog.Error("error scanning float to numeric", "error", err)
slog.Error("error scanning numeric", "error", err)
}
return value
}
func AddNumeric(a, b pgtype.Numeric) pgtype.Numeric {
minExp := a.Exp
if b.Exp < minExp {
minExp = b.Exp
}
aInt := new(big.Int).Set(a.Int)
bInt := new(big.Int).Set(b.Int)
for a.Exp > minExp {
aInt.Mul(aInt, big.NewInt(10))
a.Exp--
}
for b.Exp > minExp {
bInt.Mul(bInt, big.NewInt(10))
b.Exp--
}
resultado := new(big.Int).Add(aInt, bInt)
return pgtype.Numeric{
Int: resultado,
Exp: minExp,
Valid: true,
}
}
func SubtractNumeric(a, b pgtype.Numeric) pgtype.Numeric {
minExp := a.Exp
if b.Exp < minExp {
minExp = b.Exp
}
aInt := new(big.Int).Set(a.Int)
bInt := new(big.Int).Set(b.Int)
for a.Exp > minExp {
aInt.Mul(aInt, big.NewInt(10))
a.Exp--
}
for b.Exp > minExp {
bInt.Mul(bInt, big.NewInt(10))
b.Exp--
}
resultado := new(big.Int).Sub(aInt, bInt)
return pgtype.Numeric{
Int: resultado,
Exp: minExp,
Valid: true,
}
}