From ba54fe14183e144863b9bdadd7beb903d5ca86f7 Mon Sep 17 00:00:00 2001 From: vasilbekk Date: Sat, 1 Oct 2022 13:47:11 +0300 Subject: [PATCH 1/3] Added varible StringTrimTrailingZeros and comment for it --- decimal.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index c614ea79..84dd32f7 100644 --- a/decimal.go +++ b/decimal.go @@ -52,6 +52,14 @@ var DivisionPrecision = 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 @@ -1022,7 +1030,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 From d23c457004b890e4b2604217bdf9ca81365b97f7 Mon Sep 17 00:00:00 2001 From: vasilbekk Date: Sat, 1 Oct 2022 14:02:56 +0300 Subject: [PATCH 2/3] Added unittests --- decimal_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/decimal_test.go b/decimal_test.go index 2b3a99e1..14b135de 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -3339,3 +3339,51 @@ 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()) + } + } + + + +} \ No newline at end of file From 6e54f647f022cc99bd67f8fd995e7db67a2ed2df Mon Sep 17 00:00:00 2001 From: vasilbekk Date: Sat, 1 Oct 2022 14:20:16 +0300 Subject: [PATCH 3/3] Newline in end file --- decimal_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/decimal_test.go b/decimal_test.go index 14b135de..5c34a1ba 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -3383,7 +3383,4 @@ func TestDecimal_String(t *testing.T) { t.Errorf("expected %s, got %s", test.expected, d.String()) } } - - - -} \ No newline at end of file +}