Skip to content

Commit

Permalink
change name to major units instead of subunits
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel witz committed May 5, 2019
1 parent b00401c commit f70ca80
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (f *Formatter) Format(amount int64) string {
return sa
}

// ToSubunits returns float64 representing the value in sub units using the currency data
func (f *Formatter) ToSubunits(amount int64) float64 {
// ToMajorUnits returns float64 representing the value in sub units using the currency data
func (f *Formatter) ToMajorUnits(amount int64) float64 {
return float64(amount) / float64(math.Pow10(f.Fraction))
}

Expand Down
6 changes: 3 additions & 3 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestFormatter_Format(t *testing.T) {
}
}

func TestFormatter_ToSubunits(t *testing.T) {
func TestFormatter_ToMajorUnits(t *testing.T) {
tcs := []struct {
fraction int
decimal string
Expand Down Expand Up @@ -160,10 +160,10 @@ func TestFormatter_ToSubunits(t *testing.T) {

for _, tc := range tcs {
formatter := NewFormatter(tc.fraction, tc.decimal, tc.thousand, tc.grapheme, tc.template)
r := formatter.ToSubunits(tc.amount)
r := formatter.ToMajorUnits(tc.amount)

if r != tc.expected {
t.Errorf("Expected %d formatted to sub units to be %f got %f", tc.amount, tc.expected, r)
t.Errorf("Expected %d formatted to major units to be %f got %f", tc.amount, tc.expected, r)
}
}
}
6 changes: 3 additions & 3 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ func (m *Money) Display() string {
return c.Formatter().Format(m.amount.val)
}

// AsSubUnits lets represent Money struct as subunits (float64) in given Currency value
func (m *Money) AsSubunits() float64 {
// AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
func (m *Money) AsMajorUnits() float64 {
c := m.currency.get()
return c.Formatter().ToSubunits(m.amount.val)
return c.Formatter().ToMajorUnits(m.amount.val)
}
6 changes: 3 additions & 3 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func TestMoney_Display(t *testing.T) {
}
}

func TestMoney_AsSubunits(t *testing.T) {
func TestMoney_AsMajorUnits(t *testing.T) {
tcs := []struct {
amount int64
code string
Expand All @@ -577,10 +577,10 @@ func TestMoney_AsSubunits(t *testing.T) {

for _, tc := range tcs {
m := New(tc.amount, tc.code)
r := m.AsSubunits()
r := m.AsMajorUnits()

if r != tc.expected {
t.Errorf("Expected value as subunits of %d to be %f got %f", tc.amount, tc.expected, r)
t.Errorf("Expected value as major units of %d to be %f got %f", tc.amount, tc.expected, r)
}
}
}
Expand Down

0 comments on commit f70ca80

Please sign in to comment.