From eb2f9949b6ec3f9660d6151ed27ecc1270bafe2c Mon Sep 17 00:00:00 2001 From: sqshq Date: Thu, 23 May 2019 23:51:37 -0400 Subject: [PATCH] formatting fix and test added --- component/util/format.go | 4 +++- component/util/format_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 component/util/format_test.go diff --git a/component/util/format.go b/component/util/format.go index 1cec09c..09e58bd 100644 --- a/component/util/format.go +++ b/component/util/format.go @@ -71,7 +71,9 @@ func formatTrailingDigits(value string, scale int) string { formatted = value[:i+scale] } - return strings.TrimRight(formatted, "0.") + formatted = strings.TrimRight(formatted, "0") + + return strings.TrimRight(formatted, ".") } return value diff --git a/component/util/format_test.go b/component/util/format_test.go new file mode 100644 index 0000000..e9c6854 --- /dev/null +++ b/component/util/format_test.go @@ -0,0 +1,27 @@ +package util + +import "testing" + +func TestFormatValue(t *testing.T) { + type args struct { + value float64 + scale int + } + tests := []struct { + name string + args args + want string + }{ + {"should format float value with scale = 1", args{94.123, 1}, "94.1"}, + {"should format float value with scale = 0", args{94.123, 0}, "94"}, + {"should format float value with trailing zeros", args{94.100, 5}, "94.1"}, + {"should format float value with radix char and trailing zeros", args{9423000.00123, 2}, "9,423,000"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FormatValue(tt.args.value, tt.args.scale); got != tt.want { + t.Errorf("FormatValue() = %v, want %v", got, tt.want) + } + }) + } +}