Skip to content

Commit

Permalink
Changed option var to TrimTrailingZeros and it's description
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnAD committed Dec 9, 2024
1 parent 891b37d commit e11ab9a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 6 additions & 8 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ 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
// TrimTrailingZeros specifies whether trailing zeroes should be trimmed from a string representation of decimal.
// If set to true, trailing zeroes will be truncated (2.00 -> 2, 3.11 -> 3.11, 13.000 -> 13),
// otherwise trailing zeroes will be preserved (2.00 -> 2.00, 3.11 -> 3.11, 13.000 -> 13.000).
// Setting this value to false can be useful for APIs where exact decimal string representation matters.
var TrimTrailingZeros = true

// ExpMaxIterations specifies the maximum number of iterations needed to calculate
// precise natural exponent value using ExpHullAbrham method.
Expand Down Expand Up @@ -1477,7 +1475,7 @@ func (d Decimal) InexactFloat64() float64 {
//
// -12.345
func (d Decimal) String() string {
return d.string(StringTrimTrailingZeros)
return d.string(TrimTrailingZeros)
}

// StringFixed returns a rounded fixed-point string with places digits after
Expand Down
10 changes: 5 additions & 5 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3650,7 +3650,7 @@ func ExampleNewFromFloat() {

func TestDecimal_String(t *testing.T) {
type testData struct {
input string
input string
expected string
}

Expand All @@ -3664,7 +3664,7 @@ func TestDecimal_String(t *testing.T) {
}

for _, test := range tests {
d, err := NewFromString(test.input);
d, err := NewFromString(test.input)
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
Expand All @@ -3673,18 +3673,18 @@ func TestDecimal_String(t *testing.T) {
}

defer func() {
StringTrimTrailingZeros = true
TrimTrailingZeros = true
}()

StringTrimTrailingZeros = false
TrimTrailingZeros = false
tests = []testData{
{"1.00", "1.00"},
{"0.00", "0.00"},
{"129.123000", "129.123000"},
}

for _, test := range tests {
d, err := NewFromString(test.input);
d, err := NewFromString(test.input)
if err != nil {
t.Fatal(err)
} else if d.String() != test.expected {
Expand Down

0 comments on commit e11ab9a

Please sign in to comment.