Skip to content

Commit

Permalink
Merge pull request #29 from fluidpay/develop
Browse files Browse the repository at this point in the history
merge dev into master
  • Loading branch information
stevemkroll authored Feb 26, 2020
2 parents cfa7c0d + fbd0f26 commit 6e721a9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion currency_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 16 additions & 3 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
40 changes: 33 additions & 7 deletions function_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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},
Expand All @@ -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)
Expand All @@ -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},
Expand All @@ -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)
Expand Down

0 comments on commit 6e721a9

Please sign in to comment.