diff --git a/pkg/converters/ezbookkeeping_plain_file.go b/pkg/converters/ezbookkeeping_plain_file.go index c0011296..c6a17f7e 100644 --- a/pkg/converters/ezbookkeeping_plain_file.go +++ b/pkg/converters/ezbookkeeping_plain_file.go @@ -50,7 +50,7 @@ func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator subCategory := e.replaceDelimiters(e.getTransactionSubCategoryName(transaction.CategoryId, categoryMap), separator) account := e.replaceDelimiters(e.getAccountName(transaction.AccountId, accountMap), separator) accountCurrency := e.getAccountCurrency(transaction.AccountId, accountMap) - amount := e.getDisplayAmount(transaction.Amount) + amount := utils.FormatAmount(transaction.Amount) account2 := "" account2Currency := "" account2Amount := "" @@ -59,7 +59,7 @@ func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_OUT { account2 = e.replaceDelimiters(e.getAccountName(transaction.RelatedAccountId, accountMap), separator) account2Currency = e.getAccountCurrency(transaction.RelatedAccountId, accountMap) - account2Amount = e.getDisplayAmount(transaction.RelatedAccountAmount) + account2Amount = utils.FormatAmount(transaction.RelatedAccountAmount) } if transaction.GeoLongitude != 0 || transaction.GeoLatitude != 0 { @@ -139,26 +139,6 @@ func (e *EzBookKeepingPlainFileExporter) getAccountCurrency(accountId int64, acc } } -func (e *EzBookKeepingPlainFileExporter) getDisplayAmount(amount int64) string { - displayAmount := utils.Int64ToString(amount) - integer := utils.SubString(displayAmount, 0, len(displayAmount)-2) - decimals := utils.SubString(displayAmount, -2, 2) - - if integer == "" { - integer = "0" - } else if integer == "-" { - integer = "-0" - } - - if len(decimals) == 0 { - decimals = "00" - } else if len(decimals) == 1 { - decimals = "0" + decimals - } - - return integer + "." + decimals -} - func (e *EzBookKeepingPlainFileExporter) getTags(transactionId int64, allTagIndexes map[int64][]int64, tagMap map[int64]*models.TransactionTag) string { tagIndexes, exists := allTagIndexes[transactionId] diff --git a/pkg/utils/converter.go b/pkg/utils/converter.go index 2633b7d5..912236c3 100644 --- a/pkg/utils/converter.go +++ b/pkg/utils/converter.go @@ -94,3 +94,32 @@ func Float64ToString(num float64) string { func StringToFloat64(str string) (float64, error) { return strconv.ParseFloat(str, 64) } + +// FormatAmount returns a textual representation of amount +func FormatAmount(amount int64) string { + displayAmount := Int64ToString(amount) + negative := displayAmount[0] == '-' + + if negative { + displayAmount = displayAmount[1:] + } + + integer := SubString(displayAmount, 0, len(displayAmount)-2) + decimals := SubString(displayAmount, -2, 2) + + if integer == "" { + integer = "0" + } + + if len(decimals) == 0 { + decimals = "00" + } else if len(decimals) == 1 { + decimals = "0" + decimals + } + + if negative { + return "-" + integer + "." + decimals + } + + return integer + "." + decimals +} diff --git a/pkg/utils/converter_test.go b/pkg/utils/converter_test.go index 7443a23a..a51e6bad 100644 --- a/pkg/utils/converter_test.go +++ b/pkg/utils/converter_test.go @@ -122,3 +122,53 @@ func TestStringToFloat64(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, expectedValue, actualValue) } + +func TestFormatAmount(t *testing.T) { + expectedValue := "0.00" + actualValue := FormatAmount(0) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "0.00" + actualValue = FormatAmount(-0) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "0.01" + actualValue = FormatAmount(1) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "-0.01" + actualValue = FormatAmount(-1) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "0.10" + actualValue = FormatAmount(10) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "-0.10" + actualValue = FormatAmount(-10) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "0.12" + actualValue = FormatAmount(12) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "-0.12" + actualValue = FormatAmount(-12) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "1.23" + actualValue = FormatAmount(123) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "-1.23" + actualValue = FormatAmount(-123) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "12.34" + actualValue = FormatAmount(1234) + assert.Equal(t, expectedValue, actualValue) + + expectedValue = "-12.34" + actualValue = FormatAmount(-1234) + assert.Equal(t, expectedValue, actualValue) +}