From 51cbd9898de92bb4c6bad024ef35905d35a2b3e3 Mon Sep 17 00:00:00 2001 From: Andrey Muriy Date: Wed, 29 Sep 2021 10:27:36 +0300 Subject: [PATCH] Add a function to format currency value --- core/utils.go | 4 ++++ core/utils_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/core/utils.go b/core/utils.go index f7430e9..50ec066 100644 --- a/core/utils.go +++ b/core/utils.go @@ -263,3 +263,7 @@ func GetCurrencySymbol(code string) string { return strings.ToUpper(code) } + +func FormatCurrencyValue(value float32) string { + return fmt.Sprintf("%.2f", value) +} diff --git a/core/utils_test.go b/core/utils_test.go index 1750188..0e1ebb7 100644 --- a/core/utils_test.go +++ b/core/utils_test.go @@ -261,6 +261,14 @@ func TestUtils_ReplaceMarkdownSymbols(t *testing.T) { assert.Equal(t, expected, ReplaceMarkdownSymbols(test)) } +func TestUtils_FormatCurrencyValue(t *testing.T) { + assert.Equal(t, "-1.00", FormatCurrencyValue(-1)) + assert.Equal(t, "100.00", FormatCurrencyValue(100)) + assert.Equal(t, "111.11", FormatCurrencyValue(111.11)) + assert.Equal(t, "123.46", FormatCurrencyValue(123.456789)) + assert.Equal(t, "1000500.00", FormatCurrencyValue(1000500)) +} + func TestUtils_Suite(t *testing.T) { suite.Run(t, new(UtilsTest)) }