update library

This commit is contained in:
2025-05-15 14:33:40 +02:00
parent 2aa592252c
commit 155e7a4116
13 changed files with 218 additions and 260 deletions
+91
View File
@@ -3,10 +3,14 @@ package goblocks
import (
"context"
"log/slog"
"math"
"math/big"
"strconv"
"sync"
_ "github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
_ "github.com/jackc/pgx/v5/stdlib"
)
@@ -57,3 +61,90 @@ func (a *App) ClosePGXPools() {
slog.Info("closed database connection", "name", name)
}
}
func NumericToFloat64(n pgtype.Numeric) float64 {
val, err := n.Value()
if err != nil {
slog.Error("error getting numeric value", "error", err)
return 0
}
strValue, ok := val.(string)
if !ok {
slog.Error("error converting numeric value to string")
return 0
}
floatValue, err := strconv.ParseFloat(strValue, 64)
if err != nil {
slog.Error("error converting string to float", "error", err)
return 0
}
return floatValue
}
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 numeric", "error", err)
}
return value
}
func AddNumeric(a, b pgtype.Numeric) pgtype.Numeric {
minExp := min(a.Exp, 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,
}
}