forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating Decimal type to use for amounts
Rather than depending on float values which can lose precision. Created methods so Decimals can be read and written as XML (and possibly other formats that use Un/MarshalText).
- Loading branch information
Showing
6 changed files
with
148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package braintree | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const precision = 16 | ||
|
||
// Decimal represents fixed precision numbers | ||
type Decimal struct { | ||
Unscaled int64 | ||
Scale int | ||
} | ||
|
||
// NewDecimal creates a new decimal number equal to | ||
// unscaled ** 10 ^ (-scale) | ||
func NewDecimal(unscaled int64, scale int) *Decimal { | ||
return &Decimal{Unscaled: unscaled, Scale: scale} | ||
} | ||
|
||
// MarshalText outputs a decimal representation of the scaled number | ||
func (d *Decimal) MarshalText() (text []byte, err error) { | ||
b := new(bytes.Buffer) | ||
if d.Scale <= 0 { | ||
b.WriteString(strconv.FormatInt(d.Unscaled, 10)) | ||
b.WriteString(strings.Repeat("0", -d.Scale)) | ||
} else { | ||
str := strconv.FormatInt(d.Unscaled, 10) | ||
b.WriteString(str[:len(str)-d.Scale]) | ||
b.WriteString(".") | ||
b.WriteString(str[len(str)-d.Scale:]) | ||
} | ||
return b.Bytes(), nil | ||
} | ||
|
||
// UnmarshalText creates a Decimal from a string representation (e.g. 5.20) | ||
// Currently only supports decimal strings | ||
func (d *Decimal) UnmarshalText(text []byte) (err error) { | ||
var ( | ||
str = string(text) | ||
unscaled int64 = 0 | ||
scale int = 0 | ||
) | ||
|
||
if i := strings.Index(str, "."); i != -1 { | ||
scale = len(str) - i - 1 | ||
str = strings.Replace(str, ".", "", 1) | ||
} | ||
|
||
if unscaled, err = strconv.ParseInt(str, 10, 64); err != nil { | ||
return err | ||
} | ||
|
||
d.Unscaled = unscaled | ||
d.Scale = scale | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package braintree | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDecimalUnmarshalText(t *testing.T) { | ||
tests := []struct { | ||
in []byte | ||
out *Decimal | ||
shouldError bool | ||
}{ | ||
{[]byte("2.50"), NewDecimal(250, 2), false}, | ||
{[]byte("2"), NewDecimal(2, 0), false}, | ||
{[]byte("-5.504"), NewDecimal(-5504, 3), false}, | ||
{[]byte("0.5"), NewDecimal(5, 1), false}, | ||
{[]byte(".5"), NewDecimal(5, 1), false}, | ||
{[]byte("5.504.98"), NewDecimal(0, 0), true}, | ||
{[]byte("5E6"), NewDecimal(0, 0), true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
d := &Decimal{} | ||
err := d.UnmarshalText(tt.in) | ||
|
||
if tt.shouldError { | ||
if err == nil { | ||
t.Errorf("expected UnmarshalText(%s) => to error, but it did not", tt.in) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected UnmarshalText(%s) => to not error, but it did with %s", tt.in, err) | ||
} | ||
} | ||
|
||
if !reflect.DeepEqual(d, tt.out) { | ||
t.Errorf("UnmarshalText(%s) => %+v, want %+v", tt.in, d, tt.out) | ||
} | ||
} | ||
} | ||
|
||
func TestDecimalMarshalText(t *testing.T) { | ||
tests := []struct { | ||
in *Decimal | ||
out []byte | ||
}{ | ||
{NewDecimal(250, -2), []byte("25000")}, | ||
{NewDecimal(2, 0), []byte("2")}, | ||
{NewDecimal(250, 2), []byte("2.50")}, | ||
{NewDecimal(4586, 2), []byte("45.86")}, | ||
{NewDecimal(-5504, 2), []byte("-55.04")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
b, err := tt.in.MarshalText() | ||
if err != nil { | ||
t.Errorf("expected %+v.MarshaText() => to not error, but it did with %s", tt.in, err) | ||
} | ||
|
||
if string(tt.out) != string(b) { | ||
t.Errorf("%+v.MarshaText() => %s, want %s", tt.in, b, tt.out) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters