Skip to content

Commit

Permalink
Merge pull request #17 from fluidpay/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
stevemkroll authored Jun 3, 2019
2 parents 0d6d0ba + 2d2be6d commit fcaa7d3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Dough
![Dough](/images/dough.svg)
![Dough](/dough.svg)

"Roll Out" your golang currency issues with Dough.

Expand Down
28 changes: 18 additions & 10 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ func StringToInt(num string, alpha string) (int, error) {
return 0, err
}

// Find all numbers and a decimal
reg := regexp.MustCompile("[-0-9" + ISO.Decimal + "]+")
strArray := reg.FindAllString(num, -1)
str := strings.Join(strArray, "")
isNegative := strings.Contains(num, "-")

// If using a different decimal type replace with period
str = strings.Replace(str, ISO.Decimal, ".", -1)
reg := regexp.MustCompile("[0-9]+")
str := reg.FindAllString(num, -1)
strJoin := strings.Join(str, "")
fl, err := strconv.ParseFloat(strJoin, 64)

// Take array of found matches and create float
fl, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, ErrorUnableToFormatCurrencyFromString
}

// Return a mulitple of the fraction to give us our uint
if strings.Contains(num, ISO.Decimal) == true {
split := strings.Split(num, ISO.Decimal)
if len(split[1]) != ISO.Fraction {
return 0, ErrorInvalidStringFormat
}
if isNegative {
return int(fl) * -1, nil
}
return int(fl), nil
}
if isNegative {
return int(fl*math.Pow10(ISO.Fraction)) * -1, nil
}
return int(fl * math.Pow10(ISO.Fraction)), nil
}

Expand Down
33 changes: 27 additions & 6 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"testing"
)

func TestCurrencyCount(t *testing.T) {
num := len(CurrencyList)
t.Log("Currency Count: ", num)
}
// Enable to view currency count

// func TestCurrencyCount(t *testing.T) {
// num := len(CurrencyList)
// t.Log("Currency Count: ", num)
// }

var TestStringToIntData = []struct {
Num string
Expand All @@ -21,10 +23,12 @@ var TestStringToIntData = []struct {
{"$5", "USA", ErrorInvalidISO.Error()},
{"$5", "USD", 500},
{"$500", "USD", 50000},
{"$ -500", "USD", -50000},
{"$-500", "USD", -50000},
{"$05", "USD", 500},
{"$0.05", "USD", 5},
{"$5.0", "USD", 500},
{"$0.0.5", "USD", ErrorInvalidStringFormat.Error()},
{"$5.0", "USD", ErrorInvalidStringFormat.Error()},
{"$5.000", "USD", ErrorInvalidStringFormat.Error()},
{"$5.52", "USD", 552},
{"$0.00", "USD", 0},
{"$0.01", "USD", 1},
Expand All @@ -37,6 +41,23 @@ var TestStringToIntData = []struct {
{"$100,000.00", "USD", 10000000},
{"$1,000,000.00", "USD", 100000000},

// Problematic Numbers
{"$538.92", "USD", 53892},
{"$65.85", "USD", 6585},
{"$17.99", "USD", 1799},
{"538.92", "USD", 53892},
{"65.85", "USD", 6585},
{"17.99", "USD", 1799},
{"$-538.92", "USD", -53892},
{"$-65.85", "USD", -6585},
{"$-17.99", "USD", -1799},
{"-538.92", "USD", -53892},
{"-65.85", "USD", -6585},
{"-17.99", "USD", -1799},
{"-$538.92", "USD", -53892},
{"-$65.85", "USD", -6585},
{"-$17.99", "USD", -1799},

// Non USD
{"$100.00,00", "ARS", 1000000},
{"$10,000,000", "JPY", 10000000},
Expand Down
File renamed without changes
3 changes: 3 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "errors"
// ErrorInvalidISO : returns an error for an invalid ISO code
var ErrorInvalidISO = errors.New("Invalid ISO Code")

// ErrorInvalidStringFormat : returns an error if trying to convert an invalid string value
var ErrorInvalidStringFormat = errors.New("Unable To Convert Invalid String Format")

// ErrorUnableToFormatCurrency : returns an error for invalid currency formatting
var ErrorUnableToFormatCurrency = errors.New("Unable To Format Currency")

Expand Down

0 comments on commit fcaa7d3

Please sign in to comment.