Add some unit-tests for numeric
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2022-02-23 21:52:36 +00:00
parent 28c20aacd3
commit ea6d198bff
2 changed files with 104 additions and 3 deletions

View File

@ -17,6 +17,19 @@ func Zero() Numeric {
return Numeric{pgtype.Numeric{Exp: 0, Int: big.NewInt(0), Status: pgtype.Present, NaN: false}}
}
func FromInt64(value int64) Numeric {
num := Numeric{}
num.Set(value)
return num
}
func FromInt64WithExp(value int64, exp int32) Numeric {
num := Numeric{}
num.Set(value)
num.Exp = exp
return num
}
func (n Numeric) GetFloat64() float64 {
if n.Status != pgtype.Present {
return 0
@ -168,10 +181,16 @@ func (n Numeric) MarshalJSON() ([]byte, error) {
return bytesWithSeparator, nil
}
func Parse(text string) (Numeric, error) {
// Remove trailing currency
text = trimLastChar(text)
func MustParse(text string) Numeric {
num, err := Parse(text)
if err != nil {
panic(err)
}
return num
}
func Parse(text string) (Numeric, error) {
// Unify decimal separator
text = strings.Replace(text, ",", ".", 1)
@ -184,6 +203,13 @@ func Parse(text string) (Numeric, error) {
return num, nil
}
func ParseCurrency(text string) (Numeric, error) {
// Remove trailing currency
text = trimLastChar(text)
return Parse(text)
}
func trimLastChar(s string) string {
r, size := utf8.DecodeLastRuneInString(s)
if r == utf8.RuneError && (size == 0 || size == 1) {