Skip to content

Commit

Permalink
Make fixed discount amount a string for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
biilmann committed May 18, 2017
1 parent 4242a1f commit ebc546e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
6 changes: 4 additions & 2 deletions calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package calculator

import (
"math"
"strconv"

"github.com/netlify/gocommerce/claims"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ type taxAmount struct {
}

type FixedMemberDiscount struct {
Amount uint64 `json:"amount"`
Amount string `json:"amount"`
Currency string `json:"currency"`
}

Expand Down Expand Up @@ -84,7 +85,8 @@ func (c *MemberDiscount) FixedDiscount(currency string) uint64 {
if c.FixedAmount != nil {
for _, discount := range c.FixedAmount {
if discount.Currency == currency {
return discount.Amount
amount, _ := strconv.ParseFloat(discount.Amount, 64)
return rint(amount * 100)
}
}
}
Expand Down
28 changes: 25 additions & 3 deletions models/coupon.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package models

import "time"
import (
"math"
"strconv"
"time"
)

type FixedAmount struct {
Amount uint64 `json:"amount"`
Amount string `json:"amount"`
Currency string `json:"currency"`
}

Expand Down Expand Up @@ -60,10 +64,28 @@ func (c *Coupon) FixedDiscount(currency string) uint64 {
if c.FixedAmount != nil {
for _, discount := range c.FixedAmount {
if discount.Currency == currency {
return discount.Amount
amount, _ := strconv.ParseFloat(discount.Amount, 64)
return rint(amount * 100)
}
}
}

return 0
}

// Nopes - no `round` method in go
// See https://gist.github.com/siddontang/1806573b9a8574989ccb
func rint(x float64) uint64 {
v, frac := math.Modf(x)
if x > 0.0 {
if frac > 0.5 || (frac == 0.5 && uint64(v)%2 != 0) {
v += 1.0
}
} else {
if frac < -0.5 || (frac == -0.5 && uint64(v)%2 != 0) {
v -= 1.0
}
}

return uint64(v)
}

0 comments on commit ebc546e

Please sign in to comment.