fix incorrect transaction amount in exported data
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user