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

Revert remove insignificant digits #159

Merged
merged 2 commits into from
Feb 27, 2020
Merged
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
10 changes: 3 additions & 7 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,8 @@ func NewFromString(value string) (Decimal, error) {
// an int
intString = value
} else if len(parts) == 2 {
// strip the insignificant digits for more accurate comparisons.
decimalPart := strings.TrimRight(parts[1], "0")
intString = parts[0] + decimalPart
if intString == "" && parts[1] != "" {
intString = "0"
}
expInt := -len(decimalPart)
intString = parts[0] + parts[1]
expInt := -len(parts[1])
exp += int64(expInt)
} else {
return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value)
Expand Down Expand Up @@ -870,6 +865,7 @@ func (d Decimal) RoundCash(interval uint8) Decimal {
dVal := Decimal{
value: iVal,
}

// TODO: optimize those calculations to reduce the high allocations (~29 allocs).
return d.Mul(dVal).Round(0).Div(dVal).Truncate(2)
}
Expand Down
5 changes: 3 additions & 2 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ func TestNewFromStringDeepEquals(t *testing.T) {
}
tests := []StrCmp{
{"1", "1", true},
{"10", "10.0", true},
{"1.1", "1.10", true},
{"1.0", "1.0", true},
{"10", "10.0", false},
{"1.1", "1.10", false},
{"1.001", "1.01", false},
}

Expand Down