Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed NewFromFloat to calculate accurate values #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"strconv"
"strings"

"github.com/shopspring/decimal"
)

// Formatter stores Money formatting information.
Expand Down Expand Up @@ -60,8 +62,8 @@ func (f *Formatter) ToMajorUnits(amount int64) float64 {
if f.Fraction == 0 {
return float64(amount)
}

return float64(amount) / float64(math.Pow10(f.Fraction))
u, _ := decimal.NewFromInt(amount).Div(decimal.NewFromFloat(math.Pow10(f.Fraction))).Float64()
return u
}

// abs return absolute value of given integer.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Rhymond/go-money

go 1.13

require github.com/shopspring/decimal v1.3.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
6 changes: 5 additions & 1 deletion money.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"math"

"github.com/shopspring/decimal"
)

// Injection points for backward compatibility.
Expand Down Expand Up @@ -90,7 +92,9 @@ func New(amount int64, code string) *Money {
// Always rounding trailing decimals down.
func NewFromFloat(amount float64, currency string) *Money {
currencyDecimals := math.Pow10(GetCurrency(currency).Fraction)
return New(int64(amount*currencyDecimals), currency)
newCurrencyDecimals := decimal.NewFromFloat(currencyDecimals)
newAmount := decimal.NewFromFloat(amount)
return New(newAmount.Mul(newCurrencyDecimals).IntPart(), currency)
Comment on lines 93 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "1.15" problem is related to the fact that the binary representation of 1.15 is closer to 114.99999999999999

This can solved by adding a small "epsilon" value to the parsed value.

const epsilon = 0.0001

func NewFromFloat(amount float64, currency string) *Money {
	currencyDecimals := math.Pow10(GetCurrency(currency).Fraction)
	return New(int64(amount*currencyDecimals + epsilon), currency)
}

This way it is not necessary to add a new dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another trick is to do:

func NewFromFloat(amount float64, currency string) *Money {

       var fixed float64

       if float64 >0 {
            fixed = math.Float64frombits(math.Float64bits(amount) + 1)
       } else {
            fixed = math.Float64frombits(math.Float64bits(amount) - 1)
       }

       currencyDecimals := math.Pow10(GetCurrency(currency).Fraction)

       return New(int64(fixed * currencyDecimals ), currency)
}

Here the +/- 1 is the minimum "epsilon".

Reference: https://pkg.go.dev/math#Nextafter

}

// Currency returns the currency used by Money.
Expand Down
6 changes: 6 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ func TestNewFromFloat(t *testing.T) {
t.Errorf("Expected %d got %d", 1234, m.amount)
}

m = NewFromFloat(136.98, EUR)

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

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