mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-23 04:40:49 +01:00
Revert remove insignificant digits (#159)
* revert original remove insignificant change (PR 46), and add the logic back as a function
This commit is contained in:
parent
a08b92da27
commit
02e2044944
2 changed files with 6 additions and 9 deletions
10
decimal.go
10
decimal.go
|
@ -153,13 +153,8 @@ func NewFromString(value string) (Decimal, error) {
|
||||||
// an int
|
// an int
|
||||||
intString = value
|
intString = value
|
||||||
} else if len(parts) == 2 {
|
} else if len(parts) == 2 {
|
||||||
// strip the insignificant digits for more accurate comparisons.
|
intString = parts[0] + parts[1]
|
||||||
decimalPart := strings.TrimRight(parts[1], "0")
|
expInt := -len(parts[1])
|
||||||
intString = parts[0] + decimalPart
|
|
||||||
if intString == "" && parts[1] != "" {
|
|
||||||
intString = "0"
|
|
||||||
}
|
|
||||||
expInt := -len(decimalPart)
|
|
||||||
exp += int64(expInt)
|
exp += int64(expInt)
|
||||||
} else {
|
} else {
|
||||||
return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value)
|
return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value)
|
||||||
|
@ -870,6 +865,7 @@ func (d Decimal) RoundCash(interval uint8) Decimal {
|
||||||
dVal := Decimal{
|
dVal := Decimal{
|
||||||
value: iVal,
|
value: iVal,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: optimize those calculations to reduce the high allocations (~29 allocs).
|
// TODO: optimize those calculations to reduce the high allocations (~29 allocs).
|
||||||
return d.Mul(dVal).Round(0).Div(dVal).Truncate(2)
|
return d.Mul(dVal).Round(0).Div(dVal).Truncate(2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -310,8 +310,9 @@ func TestNewFromStringDeepEquals(t *testing.T) {
|
||||||
}
|
}
|
||||||
tests := []StrCmp{
|
tests := []StrCmp{
|
||||||
{"1", "1", true},
|
{"1", "1", true},
|
||||||
{"10", "10.0", true},
|
{"1.0", "1.0", true},
|
||||||
{"1.1", "1.10", true},
|
{"10", "10.0", false},
|
||||||
|
{"1.1", "1.10", false},
|
||||||
{"1.001", "1.01", false},
|
{"1.001", "1.01", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue