Skip to content

Commit

Permalink
add NewFromFloat and NewFromString (Rhymond#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
npinochet committed Aug 2, 2022
1 parent e9a67ed commit 851067e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ Initialize Money by using smallest unit value (e.g 100 represents 1 pound). Use
```go
pound := money.New(100, money.GBP)
```

There're also some simple parsers for initializing Money from floats or strings.
```go
quarterEuro := money.NewFromFloat(0.25, money.EUR)
halfDolar := money.NewFromString("0.50", money.USD)
```
Comparison
-
**Go-money** provides base compare operations like:
Expand Down
38 changes: 38 additions & 0 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
"strings"
)

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

// NewFromFloat creates and returns new instance of Money from a float64.
// Always rounding trailing decimals down.
func NewFromFloat(amount float64, currency string) *Money {
currencyDecimals := math.Pow10(GetCurrency(currency).Fraction)
return New(int64(amount*currencyDecimals), currency)
}

// 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".
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
50 changes: 50 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,56 @@ func TestMoney_Amount(t *testing.T) {
}
}

func TestNewFromFloat(t *testing.T) {
m := NewFromFloat(12.34, EUR)

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 = NewFromFloat(-0.125, EUR)

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

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 851067e

Please sign in to comment.