Truncate should parse negative values too, or return error
Author: AlexandrosKyriakakisCreated Jul 7, 2025Updated Jul 15, 2025
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: https://github.com/shopspring/decimal/blob/08afb35517362df21914209a026f65bb33275fbc/decimal.go#L1755-L1769
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?
Source: shopspring/decimal