Closed matejsp closed 4 days ago
Thanks for reporting this. Looks like I missed this case. However, please notice that this StringFixed
version behaves a little bit different than shopspring/decimal. In shopspring/decimal, StringFixed will round the result before printing, while this library just keep the result as it is (any rouding should be performed before calling this method)
// shopspring/decimal
a := decimal.RequireFromString("1.555")
fmt.Println(a.StringFixed(2)) // 1.56
// udecimal
b := udecimal.MustParse("1.555")
fmt.Println(b.StringFixed(2)) // 1.555
Yes I noticed that I need to round before and that's fine. Another difference I noticed that shopspring supports negative precision (so you can round to -1 => 10s or -2 => 100s). But your round supports only rounding on decimal part.
// StringFixed returns a rounded fixed-point string with places digits after
// the decimal point.
//
// Example:
//
// NewFromFloat(0).StringFixed(2) // output: "0.00"
// NewFromFloat(0).StringFixed(0) // output: "0"
// NewFromFloat(5.45).StringFixed(0) // output: "5"
// NewFromFloat(5.45).StringFixed(1) // output: "5.5"
// NewFromFloat(5.45).StringFixed(2) // output: "5.45"
// NewFromFloat(5.45).StringFixed(3) // output: "5.450"
// NewFromFloat(545).StringFixed(-1) // output: "550"
func (d Decimal) StringFixed(places int32) string {
rounded := d.Round(places)
return rounded.string(false)
}
All rounding methods don't support negative value. I'm not sure if rounding to power of 10 is necessary or not. Might be an improvement for v2 if it's actually needed.
StringFixed should return with fixed precision but it is not working for fixed numbers as expected.