Skip to content

Commit

Permalink
add NewFromString #108
Browse files Browse the repository at this point in the history
  • Loading branch information
npinochet committed Aug 2, 2022
1 parent e9a67ed commit d89be76
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Initialize Money by using smallest unit value (e.g 100 represents 1 pound). Use
```go
pound := money.New(100, money.GBP)
```
Or initialize Money using the direct amount as a string.
```go
twoFiftyDolars := money.NewFromString("2.50", money.USD)
```
Comparison
-
**Go-money** provides base compare operations like:
Expand Down
30 changes: 30 additions & 0 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
)

// Injection points for backward compatibility.
Expand Down Expand Up @@ -85,6 +87,34 @@ func New(amount int64, code string) *Money {
}
}

// NewFromString creates and returns new instance of Money from a string.
// Can only parse simple float-like strings, like "1.23", not "1.23 USD", "$1.23" or "1,000" USD.
func NewFromString(amount string, currencyCode string) (*Money, error) {
currency := GetCurrency(currencyCode)
fraction := currency.Fraction

toParse := amount
var decimals int
if pointIndex := strings.Index(amount, currency.Decimal); pointIndex != -1 {
decimals = len(amount) - pointIndex - 1
if decimals > fraction {
decimals = fraction
}
toParse = amount[:pointIndex] + amount[pointIndex+1:pointIndex+1+decimals]
}

parsed, err := strconv.ParseInt(toParse, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't parse '%s' to money", amount)
}

for d := decimals; d < fraction; d++ {
parsed *= 10
}

return New(parsed, currencyCode), nil
}

// Currency returns the currency used by Money.
func (m *Money) Currency() *Currency {
return m.currency
Expand Down
32 changes: 32 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,38 @@ func TestMoney_Amount(t *testing.T) {
}
}

func TestNewFromString(t *testing.T) {
m, err := NewFromString("12.34", EUR)

if err != nil {
t.Error(err)
}

if m.amount != 1234 {
t.Errorf("Expected %d got %d", 1234, m.amount)
}

if m.currency.Code != EUR {
t.Errorf("Expected currency %s got %s", EUR, m.currency.Code)
}

m, err = NewFromString("-1.12345", EUR)

if err != nil {
t.Error(err)
}

if m.amount != -112 {
t.Errorf("Expected %d got %d", -112, m.amount)
}

_, err = NewFromString("invalid_input", EUR)

if err.Error() != "can't parse 'invalid_input' to money" {
t.Error(err)
}
}

func TestDefaultMarshal(t *testing.T) {
given := New(12345, IQD)
expected := `{"amount":12345,"currency":"IQD"}`
Expand Down

0 comments on commit d89be76

Please sign in to comment.