decimal
decimal copied to clipboard
Exponent() for NewFromFloat returns incorect value.
Step to reproduce:
var price float64 = 99.00
decPrice:=decimal.NewFromFloat(price )
FractionalPart := decPrice.Exponent()
AR: FractionalPart is 0 ER: FractionalPart 2
The fraction gets tossed away by Go, so decimal package only gets the 99 value:
fmt.Println(99.00) // 99
fmt.Println(99.00 == 99) // true
If you want to keep the exponent, use a string instead:
d := decimal.RequireFromString("99.00")
fmt.Println(d.Exponent()) // -2
https://go.dev/play/p/65l7AF7YGyT