Truncate function would be useful to parse also negative values. So for example,
a := decimal.RequireFromString("5432")
b := a.Truncate(-2)
So b should return 5000, currently it returns a which is misleading. In my opinion it should either return 5000 or an error.
I don't see any reason having this check precision >= 0 here:
|
// Truncate truncates off digits from the number, without rounding. |
|
// |
|
// NOTE: precision is the last digit that will not be truncated (must be >= 0). |
|
// |
|
// Example: |
|
// |
|
// decimal.NewFromString("123.456").Truncate(2).String() // "123.45" |
|
func (d Decimal) Truncate(precision int32) Decimal { |
|
d.ensureInitialized() |
|
if precision >= 0 && -precision > d.exp { |
|
return d.rescale(-precision) |
|
} |
|
return d |
|
} |
|
|
func (d Decimal) Truncate(precision int32) Decimal {
d.ensureInitialized()
if precision >= 0 && -precision > d.exp {
return d.rescale(-precision)
}
return d
}
Since it would work, right out of the box.
Any thoughts or concerns?
Truncate function would be useful to parse also negative values. So for example,
So
bshould return5000, currently it returnsawhich is misleading. In my opinion it should either return5000or an error.I don't see any reason having this check
precision >= 0here:decimal/decimal.go
Lines 1755 to 1769 in 08afb35
Since it would work, right out of the box.
Any thoughts or concerns?