Skip to content

Commit

Permalink
Account for case with too many fractional digits
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed May 13, 2019
1 parent 6c9684d commit 33d1050
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion xmath/fixed/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func Parse(str string) (Fixed, error) {
for buffer.Len() < precision+1 {
buffer.WriteString("0")
}
if fraction, err = strconv.ParseInt(buffer.String(), 10, 64); err != nil {
frac := buffer.String()
if len(frac) > precision+1 {
frac = frac[:precision+1]
}
if fraction, err = strconv.ParseInt(frac, 10, 64); err != nil {
return 0, errs.Wrap(err)
}
value += fraction - multiplier
Expand Down
8 changes: 8 additions & 0 deletions xmath/fixed/fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func TestConversion(t *testing.T) {
assert.Equal(t, "-1", fixed.FromFloat64(-1.00009).String())
assert.Equal(t, "0.0004", fixed.FromFloat64(0.000405).String())
assert.Equal(t, "-0.0004", fixed.FromFloat64(-0.000405).String())

v, err := fixed.Parse("33.0")
assert.NoError(t, err)
assert.Equal(t, v, fixed.FromInt(33))

v, err = fixed.Parse("33.00000000000000000000")
assert.NoError(t, err)
assert.Equal(t, v, fixed.FromInt(33))
}

func TestAddSub(t *testing.T) {
Expand Down

0 comments on commit 33d1050

Please sign in to comment.