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,
}
}
+78
View File
@@ -0,0 +1,78 @@
package pgutils
import (
"fmt"
"log/slog"
"math/big"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/stretchr/testify/assert"
)
func Test_NumericToFloat(t *testing.T) {
tests := []struct {
given pgtype.Numeric
expected float64
}{
{given: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Valid: true}, expected: 0},
{given: pgtype.Numeric{Int: big.NewInt(5), Exp: 0, Valid: true}, expected: 5},
{given: pgtype.Numeric{Int: big.NewInt(10), Exp: 0, Valid: true}, expected: 10},
{given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Valid: true}, expected: 10.00},
{given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -3, Valid: true}, expected: 1.000},
{given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -4, Valid: true}, expected: 0.1000},
{given: pgtype.Numeric{Int: big.NewInt(2555), Exp: -2, Valid: true}, expected: 25.55},
{given: pgtype.Numeric{Int: big.NewInt(-1), Exp: -2, Valid: true}, expected: -0.01},
}
for _, test := range tests {
t.Run(fmt.Sprintf("given %v, expected %v", test.given, test.expected), func(t *testing.T) {
actual := NumericToFloat64(test.given)
assert.Equal(t, test.expected, actual)
})
}
}
func Test_FloatToNumeric(t *testing.T) {
tests := []struct {
given float64
expected pgtype.Numeric
precision int
}{
{given: 0.0, expected: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Valid: true}, precision: 0},
{given: 25.50, expected: pgtype.Numeric{Int: big.NewInt(2550), Exp: -2, Valid: true}, precision: 2},
{given: 100.0, expected: pgtype.Numeric{Int: big.NewInt(1), Exp: 2, Valid: true}, precision: 0},
{given: 0.0001, expected: pgtype.Numeric{Int: big.NewInt(0), Exp: -2, Valid: true}, precision: 2},
{given: 0.0001, expected: pgtype.Numeric{Int: big.NewInt(1), Exp: -4, Valid: true}, precision: 4},
}
for _, test := range tests {
t.Run(fmt.Sprintf("given %v, expected %v", test.given, test.expected), func(t *testing.T) {
actual := FloatToNumeric(test.given, test.precision)
assert.Equal(t, test.expected, actual)
})
}
}
func Test_AddNumeric(t *testing.T) {
valueA := pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Valid: true}
valueB := pgtype.Numeric{Int: big.NewInt(2), Exp: -2, Valid: true}
slog.Info("valueA", "valueA", valueA)
slog.Info("valueB", "valueB", valueB)
actual := AddNumeric(valueA, valueB)
slog.Info("actual", "actual", actual)
assert.Equal(t, pgtype.Numeric{Int: big.NewInt(21), Exp: -3, Valid: true}, actual)
}
func Test_SubtractNumeric(t *testing.T) {
valueA := pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Valid: true}
valueB := pgtype.Numeric{Int: big.NewInt(2), Exp: -2, Valid: true}
actual := SubtractNumeric(valueA, valueB)
slog.Info("actual", "actual", actual)
assert.Equal(t, pgtype.Numeric{Int: big.NewInt(-19), Exp: -3, Valid: true}, actual)
}