Add some unit-tests for numeric
This commit is contained in:
@ -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) {
|
||||
|
Reference in New Issue
Block a user