mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
Compare commits
5 commits
96726f737e
...
d2fc310165
Author | SHA1 | Date | |
---|---|---|---|
|
d2fc310165 | ||
|
80ec940054 | ||
|
6e54f647f0 | ||
|
d23c457004 | ||
|
ba54fe1418 |
2 changed files with 95 additions and 9 deletions
22
decimal.go
22
decimal.go
|
@ -65,6 +65,14 @@ var PowPrecisionNegativeExponent = 16
|
|||
// silently lose precision.
|
||||
var MarshalJSONWithoutQuotes = false
|
||||
|
||||
// StringTrimTrailingZeros should be set to false if you want the decimal stringify without zeros trailing.
|
||||
// By default, when decimal is output as a string (for example, in JSON), zeros are truncated from it (2.00 -> 2, 3.11 -> 3.11, 13.000 -> 13).
|
||||
// But this logic can be changed by this variable.
|
||||
// For example, if you have numeric(10,2) values stored in your database,
|
||||
// and you want your API response to always be given 2 decimal places (even 2.00, 3.00, 17.00 [not 2,3,17]),
|
||||
// then this variable is a great way out.
|
||||
var StringTrimTrailingZeros = true
|
||||
|
||||
// ExpMaxIterations specifies the maximum number of iterations needed to calculate
|
||||
// precise natural exponent value using ExpHullAbrham method.
|
||||
var ExpMaxIterations = 1000
|
||||
|
@ -130,6 +138,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
|
||||
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
||||
return Decimal{
|
||||
|
@ -1457,7 +1477,7 @@ func (d Decimal) InexactFloat64() float64 {
|
|||
//
|
||||
// -12.345
|
||||
func (d Decimal) String() string {
|
||||
return d.string(true)
|
||||
return d.string(StringTrimTrailingZeros)
|
||||
}
|
||||
|
||||
// StringFixed returns a rounded fixed-point string with places digits after
|
||||
|
|
|
@ -476,10 +476,11 @@ func TestNewFromFloatWithExponent(t *testing.T) {
|
|||
|
||||
func TestNewFromInt(t *testing.T) {
|
||||
tests := map[int64]string{
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
9223372036854775807: "9223372036854775807",
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
9223372036854775807: "9223372036854775807",
|
||||
-9223372036854775808: "-9223372036854775808",
|
||||
}
|
||||
|
||||
// add negatives
|
||||
|
@ -501,10 +502,11 @@ func TestNewFromInt(t *testing.T) {
|
|||
|
||||
func TestNewFromInt32(t *testing.T) {
|
||||
tests := map[int32]string{
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
2147483647: "2147483647",
|
||||
0: "0",
|
||||
1: "1",
|
||||
323412345: "323412345",
|
||||
2147483647: "2147483647",
|
||||
-2147483648: "-2147483648",
|
||||
}
|
||||
|
||||
// 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) {
|
||||
type Inp struct {
|
||||
val *big.Int
|
||||
|
@ -3673,3 +3694,48 @@ func ExampleNewFromFloat() {
|
|||
//0.123123123123123
|
||||
//-10000000000000
|
||||
}
|
||||
|
||||
func TestDecimal_String(t *testing.T) {
|
||||
type testData struct {
|
||||
input string
|
||||
expected string
|
||||
}
|
||||
|
||||
tests := []testData{
|
||||
{"1.22", "1.22"},
|
||||
{"1.00", "1"},
|
||||
{"153.192", "153.192"},
|
||||
{"999.999", "999.999"},
|
||||
{"0.0000000001", "0.0000000001"},
|
||||
{"0.0000000000", "0"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d, err := NewFromString(test.input);
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if d.String() != test.expected {
|
||||
t.Errorf("expected %s, got %s", test.expected, d.String())
|
||||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
StringTrimTrailingZeros = true
|
||||
}()
|
||||
|
||||
StringTrimTrailingZeros = false
|
||||
tests = []testData{
|
||||
{"1.00", "1.00"},
|
||||
{"0.00", "0.00"},
|
||||
{"129.123000", "129.123000"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d, err := NewFromString(test.input);
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if d.String() != test.expected {
|
||||
t.Errorf("expected %s, got %s", test.expected, d.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue