Compare commits

...

5 commits

Author SHA1 Message Date
Sergey Vasiliev
96726f737e
Merge 6e54f647f0 into 2fa107d0fe 2024-04-07 19:27:51 -04:00
Mateusz Woś
2fa107d0fe
Remove production usage from docs (#362) 2024-04-07 20:33:44 +02:00
vasilbekk
6e54f647f0 Newline in end file 2022-10-01 14:20:16 +03:00
vasilbekk
d23c457004 Added unittests 2022-10-01 14:02:56 +03:00
vasilbekk
ba54fe1418 Added varible StringTrimTrailingZeros and comment for it 2022-10-01 13:47:11 +03:00
3 changed files with 54 additions and 6 deletions

View file

@ -63,11 +63,6 @@ func main() {
http://godoc.org/github.com/shopspring/decimal http://godoc.org/github.com/shopspring/decimal
## Production Usage
* [Spring](https://shopspring.com/), since August 14, 2014.
* If you are using this in production, please let us know!
## FAQ ## FAQ
#### Why don't you just use float64? #### Why don't you just use float64?

View file

@ -65,6 +65,14 @@ var PowPrecisionNegativeExponent = 16
// silently lose precision. // silently lose precision.
var MarshalJSONWithoutQuotes = false 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 // ExpMaxIterations specifies the maximum number of iterations needed to calculate
// precise natural exponent value using ExpHullAbrham method. // precise natural exponent value using ExpHullAbrham method.
var ExpMaxIterations = 1000 var ExpMaxIterations = 1000
@ -1457,7 +1465,7 @@ func (d Decimal) InexactFloat64() float64 {
// //
// -12.345 // -12.345
func (d Decimal) String() string { func (d Decimal) String() string {
return d.string(true) return d.string(StringTrimTrailingZeros)
} }
// StringFixed returns a rounded fixed-point string with places digits after // StringFixed returns a rounded fixed-point string with places digits after

View file

@ -3673,3 +3673,48 @@ func ExampleNewFromFloat() {
//0.123123123123123 //0.123123123123123
//-10000000000000 //-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())
}
}
}