mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
parent
1884f454f8
commit
c939845a68
2 changed files with 17 additions and 21 deletions
28
decimal.go
28
decimal.go
|
@ -55,9 +55,6 @@ var MarshalJSONWithoutQuotes = false
|
||||||
// Zero constant, to make computations faster.
|
// Zero constant, to make computations faster.
|
||||||
var Zero = New(0, 1)
|
var Zero = New(0, 1)
|
||||||
|
|
||||||
// fiveDec used in Cash Rounding
|
|
||||||
var fiveDec = New(5, 0)
|
|
||||||
|
|
||||||
var zeroInt = big.NewInt(0)
|
var zeroInt = big.NewInt(0)
|
||||||
var oneInt = big.NewInt(1)
|
var oneInt = big.NewInt(1)
|
||||||
var twoInt = big.NewInt(2)
|
var twoInt = big.NewInt(2)
|
||||||
|
@ -296,10 +293,9 @@ func NewFromFloatWithExponent(value float64, exp int32) Decimal {
|
||||||
// specials
|
// specials
|
||||||
if mant == 0 {
|
if mant == 0 {
|
||||||
return Decimal{}
|
return Decimal{}
|
||||||
} else {
|
|
||||||
// subnormal
|
|
||||||
exp2++
|
|
||||||
}
|
}
|
||||||
|
// subnormal
|
||||||
|
exp2++
|
||||||
} else {
|
} else {
|
||||||
// normal
|
// normal
|
||||||
mant |= 1 << 52
|
mant |= 1 << 52
|
||||||
|
@ -947,13 +943,13 @@ func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error {
|
||||||
|
|
||||||
str, err := unquoteIfQuoted(decimalBytes)
|
str, err := unquoteIfQuoted(decimalBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error decoding string '%s': %s", decimalBytes, err)
|
return fmt.Errorf("error decoding string '%s': %s", decimalBytes, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
decimal, err := NewFromString(str)
|
decimal, err := NewFromString(str)
|
||||||
*d = decimal
|
*d = decimal
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error decoding string '%s': %s", str, err)
|
return fmt.Errorf("error decoding string '%s': %s", str, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1041,7 +1037,7 @@ func (d *Decimal) UnmarshalText(text []byte) error {
|
||||||
dec, err := NewFromString(str)
|
dec, err := NewFromString(str)
|
||||||
*d = dec
|
*d = dec
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error decoding string '%s': %s", str, err)
|
return fmt.Errorf("error decoding string '%s': %s", str, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -1203,7 +1199,7 @@ func unquoteIfQuoted(value interface{}) (string, error) {
|
||||||
case []byte:
|
case []byte:
|
||||||
bytes = v
|
bytes = v
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("Could not convert value '%+v' to byte array of type '%T'",
|
return "", fmt.Errorf("could not convert value '%+v' to byte array of type '%T'",
|
||||||
value, value)
|
value, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1260,14 +1256,14 @@ func (d NullDecimal) MarshalJSON() ([]byte, error) {
|
||||||
// Trig functions
|
// Trig functions
|
||||||
|
|
||||||
// Atan returns the arctangent, in radians, of x.
|
// Atan returns the arctangent, in radians, of x.
|
||||||
func (x Decimal) Atan() Decimal {
|
func (d Decimal) Atan() Decimal {
|
||||||
if x.Equal(NewFromFloat(0.0)) {
|
if d.Equal(NewFromFloat(0.0)) {
|
||||||
return x
|
return d
|
||||||
}
|
}
|
||||||
if x.GreaterThan(NewFromFloat(0.0)) {
|
if d.GreaterThan(NewFromFloat(0.0)) {
|
||||||
return x.satan()
|
return d.satan()
|
||||||
}
|
}
|
||||||
return x.Neg().satan().Neg()
|
return d.Neg().satan().Neg()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Decimal) xatan() Decimal {
|
func (d Decimal) xatan() Decimal {
|
||||||
|
|
|
@ -358,7 +358,7 @@ func TestRequireFromStringErrs(t *testing.T) {
|
||||||
err = recover()
|
err = recover()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
d = RequireFromString(s)
|
RequireFromString(s)
|
||||||
}(d)
|
}(d)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -1404,8 +1404,8 @@ func createDivTestCases() []DivTestCase {
|
||||||
for _, v2 := range a { // 11, even if 0 is skipped
|
for _, v2 := range a { // 11, even if 0 is skipped
|
||||||
sign1 := New(int64(s), 0)
|
sign1 := New(int64(s), 0)
|
||||||
sign2 := New(int64(s2), 0)
|
sign2 := New(int64(s2), 0)
|
||||||
d := sign1.Mul(New(int64(v1), int32(e1)))
|
d := sign1.Mul(New(int64(v1), e1))
|
||||||
d2 := sign2.Mul(New(int64(v2), int32(e2)))
|
d2 := sign2.Mul(New(int64(v2), e2))
|
||||||
res = append(res, DivTestCase{d, d2, prec})
|
res = append(res, DivTestCase{d, d2, prec})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1842,7 +1842,7 @@ func TestDecimal_Scan(t *testing.T) {
|
||||||
// in normal operations the db driver (sqlite at least)
|
// in normal operations the db driver (sqlite at least)
|
||||||
// will return an int64 if you specified a numeric format
|
// will return an int64 if you specified a numeric format
|
||||||
a := Decimal{}
|
a := Decimal{}
|
||||||
dbvalue := float64(54.33)
|
dbvalue := 54.33
|
||||||
expected := NewFromFloat(dbvalue)
|
expected := NewFromFloat(dbvalue)
|
||||||
|
|
||||||
err := a.Scan(dbvalue)
|
err := a.Scan(dbvalue)
|
||||||
|
@ -2146,7 +2146,7 @@ func TestNullDecimal_Scan(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dbvalue := float64(54.33)
|
dbvalue := 54.33
|
||||||
expected := NewFromFloat(dbvalue)
|
expected := NewFromFloat(dbvalue)
|
||||||
|
|
||||||
err = a.Scan(dbvalue)
|
err = a.Scan(dbvalue)
|
||||||
|
|
Loading…
Reference in a new issue