decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Exponent() for NewFromFloat returns incorect value.

Open voronovmaksim opened this issue 3 years ago • 1 comments

Step to reproduce:

var price float64 = 99.00
decPrice:=decimal.NewFromFloat(price )
FractionalPart := decPrice.Exponent()

AR: FractionalPart is 0 ER: FractionalPart 2

voronovmaksim avatar Feb 20 '22 19:02 voronovmaksim

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

muhlemmer avatar Feb 24 '22 22:02 muhlemmer