mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 12:30:49 +01:00
Compare commits
4 commits
558f392b50
...
5bc41f90eb
Author | SHA1 | Date | |
---|---|---|---|
|
5bc41f90eb | ||
|
547861c49b | ||
|
dd34943dc4 | ||
|
19b27aafdc |
2 changed files with 50 additions and 18 deletions
31
decimal.go
31
decimal.go
|
@ -130,6 +130,18 @@ func NewFromInt32(value int32) Decimal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromUint64 converts an uint64 to Decimal.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// NewFromUint64(123).String() // output: "123"
|
||||||
|
func NewFromUint64(value uint64) Decimal {
|
||||||
|
return Decimal{
|
||||||
|
value: new(big.Int).SetUint64(value),
|
||||||
|
exp: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
||||||
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
||||||
return Decimal{
|
return Decimal{
|
||||||
|
@ -1784,19 +1796,18 @@ func (d *Decimal) UnmarshalBinary(data []byte) error {
|
||||||
|
|
||||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||||
func (d Decimal) MarshalBinary() (data []byte, err error) {
|
func (d Decimal) MarshalBinary() (data []byte, err error) {
|
||||||
// Write the exponent first since it's a fixed size
|
// exp is written first, but encode value first to know output size
|
||||||
v1 := make([]byte, 4)
|
var valueData []byte
|
||||||
binary.BigEndian.PutUint32(v1, uint32(d.exp))
|
if valueData, err = d.value.GobEncode(); err != nil {
|
||||||
|
return nil, err
|
||||||
// Add the value
|
|
||||||
var v2 []byte
|
|
||||||
if v2, err = d.value.GobEncode(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write the exponent in front, since it's a fixed size
|
||||||
|
expData := make([]byte, 4, len(valueData)+4)
|
||||||
|
binary.BigEndian.PutUint32(expData, uint32(d.exp))
|
||||||
|
|
||||||
// Return the byte array
|
// Return the byte array
|
||||||
data = append(v1, v2...)
|
return append(expData, valueData...), nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan implements the sql.Scanner interface for database deserialization.
|
// Scan implements the sql.Scanner interface for database deserialization.
|
||||||
|
|
|
@ -476,10 +476,11 @@ func TestNewFromFloatWithExponent(t *testing.T) {
|
||||||
|
|
||||||
func TestNewFromInt(t *testing.T) {
|
func TestNewFromInt(t *testing.T) {
|
||||||
tests := map[int64]string{
|
tests := map[int64]string{
|
||||||
0: "0",
|
0: "0",
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
9223372036854775807: "9223372036854775807",
|
9223372036854775807: "9223372036854775807",
|
||||||
|
-9223372036854775808: "-9223372036854775808",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -501,10 +502,11 @@ func TestNewFromInt(t *testing.T) {
|
||||||
|
|
||||||
func TestNewFromInt32(t *testing.T) {
|
func TestNewFromInt32(t *testing.T) {
|
||||||
tests := map[int32]string{
|
tests := map[int32]string{
|
||||||
0: "0",
|
0: "0",
|
||||||
1: "1",
|
1: "1",
|
||||||
323412345: "323412345",
|
323412345: "323412345",
|
||||||
2147483647: "2147483647",
|
2147483647: "2147483647",
|
||||||
|
-2147483648: "-2147483648",
|
||||||
}
|
}
|
||||||
|
|
||||||
// add negatives
|
// add negatives
|
||||||
|
@ -524,6 +526,25 @@ func TestNewFromInt32(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewFromUint64(t *testing.T) {
|
||||||
|
tests := map[uint64]string{
|
||||||
|
0: "0",
|
||||||
|
1: "1",
|
||||||
|
323412345: "323412345",
|
||||||
|
9223372036854775807: "9223372036854775807",
|
||||||
|
18446744073709551615: "18446744073709551615",
|
||||||
|
}
|
||||||
|
|
||||||
|
for input, s := range tests {
|
||||||
|
d := NewFromUint64(input)
|
||||||
|
if d.String() != s {
|
||||||
|
t.Errorf("expected %s, got %s (%s, %d)",
|
||||||
|
s, d.String(),
|
||||||
|
d.value.String(), d.exp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewFromBigIntWithExponent(t *testing.T) {
|
func TestNewFromBigIntWithExponent(t *testing.T) {
|
||||||
type Inp struct {
|
type Inp struct {
|
||||||
val *big.Int
|
val *big.Int
|
||||||
|
|
Loading…
Reference in a new issue