mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Add implementation of CoefficientInt64 method (#244)
* Add implementation of CoefficientInt64 method
This commit is contained in:
parent
483a047db9
commit
b11a7c46d7
2 changed files with 39 additions and 3 deletions
12
decimal.go
12
decimal.go
|
@ -756,14 +756,20 @@ func (d Decimal) Exponent() int32 {
|
||||||
return d.exp
|
return d.exp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
|
// Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent()
|
||||||
func (d Decimal) Coefficient() *big.Int {
|
func (d Decimal) Coefficient() *big.Int {
|
||||||
d.ensureInitialized()
|
d.ensureInitialized()
|
||||||
// we copy the coefficient so that mutating the result does not mutate the
|
// we copy the coefficient so that mutating the result does not mutate the Decimal.
|
||||||
// Decimal.
|
|
||||||
return big.NewInt(0).Set(d.value)
|
return big.NewInt(0).Set(d.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent()
|
||||||
|
// If coefficient cannot be represented in an int64, the result will be undefined.
|
||||||
|
func (d Decimal) CoefficientInt64() int64 {
|
||||||
|
d.ensureInitialized()
|
||||||
|
return d.value.Int64()
|
||||||
|
}
|
||||||
|
|
||||||
// IntPart returns the integer component of the decimal.
|
// IntPart returns the integer component of the decimal.
|
||||||
func (d Decimal) IntPart() int64 {
|
func (d Decimal) IntPart() int64 {
|
||||||
scaledD := d.rescale(0)
|
scaledD := d.rescale(0)
|
||||||
|
|
|
@ -2643,6 +2643,36 @@ func TestDecimal_Coefficient(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecimal_CoefficientInt64(t *testing.T) {
|
||||||
|
type Inp struct {
|
||||||
|
Dec string
|
||||||
|
Coefficient int64
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []Inp{
|
||||||
|
{"1", 1},
|
||||||
|
{"1.111", 1111},
|
||||||
|
{"1.000000", 1000000},
|
||||||
|
{"1.121215125511", 1121215125511},
|
||||||
|
{"100000000000000000", 100000000000000000},
|
||||||
|
{"9223372036854775807", 9223372036854775807},
|
||||||
|
{"10000000000000000000", -8446744073709551616}, // undefined value
|
||||||
|
}
|
||||||
|
|
||||||
|
// add negative cases
|
||||||
|
for _, tc := range testCases {
|
||||||
|
testCases = append(testCases, Inp{"-" + tc.Dec, -tc.Coefficient})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
d := RequireFromString(tc.Dec)
|
||||||
|
coefficient := d.CoefficientInt64()
|
||||||
|
if coefficient != tc.Coefficient {
|
||||||
|
t.Errorf("expect coefficient %d, got %d, for decimal %s", tc.Coefficient, coefficient, tc.Dec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNullDecimal_Scan(t *testing.T) {
|
func TestNullDecimal_Scan(t *testing.T) {
|
||||||
// test the Scan method that implements the
|
// test the Scan method that implements the
|
||||||
// sql.Scanner interface
|
// sql.Scanner interface
|
||||||
|
|
Loading…
Reference in a new issue