diff --git a/currency_list.go b/currency_list.go index 692016a..8fdcac0 100644 --- a/currency_list.go +++ b/currency_list.go @@ -294,7 +294,7 @@ var CurrencyList = map[string]Currency{ Unit: "Canadian Dollar", Alpha: "CAD", Numeric: "124", - Symbol: "\u0024", + Symbol: "\u0043\u0041\u0024", Fraction: 2, Decimal: ".", Grouping: 3, diff --git a/function.go b/function.go index c025618..d39972f 100644 --- a/function.go +++ b/function.go @@ -6,7 +6,20 @@ import ( "strings" ) -// GetISOFromAlpha : returns a formatted ISO alpha code or an error if the ISO is not found +// GetISOFromNumeric : returns an ISO currency struct or an error if the ISO is not found +func GetISOFromNumeric(num string) (Currency, error) { + alpha, err := GetAlphaFromISONumeric(num) + if err != nil { + return Currency{}, ErrorInvalidISO + } + currency, err := GetISOFromAlpha(alpha) + if err != nil { + return Currency{}, ErrorInvalidISO + } + return currency, nil +} + +// GetISOFromAlpha : returns an ISO currency struct or an error if the ISO is not found func GetISOFromAlpha(alpha string) (Currency, error) { alpha = strings.ToUpper(alpha) for key := range CurrencyList { @@ -125,10 +138,10 @@ func IntToFloat(amt int, fraction int) float64 { // PercentageFromInt will give you a percentage to the exact precision that you want based on fraction func PercentageFromInt(amt int, percentage float64, fraction int) float64 { - return math.Round((float64(amt) / 100)*percentage*math.Pow10(fraction)) / math.Pow10(fraction) + return math.Round((float64(amt)/100)*percentage*math.Pow10(fraction)) / math.Pow10(fraction) } // PercentageFromFloat will give you a percentage to the exact precision that you want based on fraction func PercentageFromFloat(amt float64, percentage float64, fraction int) float64 { - return math.Round((amt / 100)*percentage*math.Pow10(fraction)) / math.Pow10(fraction) + return math.Round((amt/100)*percentage*math.Pow10(fraction)) / math.Pow10(fraction) } diff --git a/function_test.go b/function_test.go index cbd7044..48a27c3 100644 --- a/function_test.go +++ b/function_test.go @@ -1,6 +1,30 @@ package dough -import "testing" +import ( + "testing" +) + +var TestGetISOFromNumericData = []struct { + Input string + Output interface{} +}{ + {"", ErrorInvalidISO.Error()}, + {"USA", ErrorInvalidISO.Error()}, + {"840", Currency{Unit: "US Dollar", Alpha: "USD", Numeric: "840", Symbol: "$", Fraction: 2, Decimal: ".", Grouping: 3, Delimiter: ",", SymbolPositionFront: true}}, +} + +func TestGetISOFromNumeric(t *testing.T) { + for _, v := range TestGetISOFromNumericData { + result, err := GetISOFromNumeric(v.Input) + if err != nil { + if err.Error() != v.Output { + t.Error(err) + } + } else if result != v.Output { + t.Error(result) + } + } +} var TestValidateISOCodeAlphaData = []struct { Input string @@ -393,10 +417,10 @@ func BenchmarkIntToFloat(b *testing.B) { } var intPercentageData = []struct { - amt int - pct float64 + amt int + pct float64 fraction int - result float64 + result float64 }{ {898, 56.7, 2, 509.17}, {898, 56.7, 3, 509.166}, @@ -421,6 +445,7 @@ var intPercentageData = []struct { {65, .011, 3, 0.007}, {65, .011, 4, 0.0072}, } + func TestGetPercentageFromInt(t *testing.T) { for _, v := range TestLargeNums { result := PercentageFromInt(v.Integer, 1, 2) @@ -437,10 +462,10 @@ func TestGetPercentageFromInt(t *testing.T) { } var floatPercentageData = []struct { - amt float64 - pct float64 + amt float64 + pct float64 fraction int - result float64 + result float64 }{ {64.72, 10, 3, 6.472}, {64.72, 10, 2, 6.47}, @@ -464,6 +489,7 @@ var floatPercentageData = []struct { {19.93, .045, 4, .009}, {19.93, .045, 5, .00897}, } + func TestGetPercentageFromFloat(t *testing.T) { for _, v := range floatPercentageData { result := PercentageFromFloat(v.amt, v.pct, v.fraction)