decimal
decimal copied to clipboard
Arbitrary-precision fixed-point decimal numbers in Go
This adds a convenience helper function that converts a `big.Rat` to a `Decimal`. Since it is impossible to guarantee an exact accuracy, a precision attribute is used. Just as the...
Although there is omitempty tag decoration in decimal type, and the value is 0 or not fetched from table, but the result is always "0". I checked the source code,...
output 0
v1, _ := decimal.NewFromString("1") v2, _ := decimal.NewFromString("1000000000000000000") fmt.Println(v1.Div(v2))
The current implementation of `Pow` only looks at the `IntPart` of the exponent, so something like 4^2.5 returns 16 :( I'm not sure why the exponent argument is supposed to...
Example: ``` a, _ := decimal.NewFromString("166.6666666666666667") b := decimal.NewFromFloat(166.6666666666666667) fmt.Println(a.Equal(b)) // false println(a.String()) // 166.6666666666666667 println(b.String()) // 166.66666666666666 ``` Play with it: https://go.dev/play/p/isV9ODqEy5u The same for: ``` decimal.NewFromFloatWithExponent(166.6666666666666667, -16) ```...
```go fee := decimal.NewFromFloat(0.0295) feeMin := decimal.NewFromFloat(4) feeAmt := decimal.NewFromFloat(2.95) fmt.Println((4 / 2.95) * 0.0295) fmt.Println(feeMin.Div(feeAmt).Mul(fee).String()) fmt.Println(feeMin.Mul(fee.Div(feeAmt)).String()) ``` ```bash 0.04 0.03999999999999999975 0.04 ``` This example: https://play.golang.org/p/qaAtlBHTJpY
```go v, err := decimal.NewFromString("") // err == nil // v == decimal.Zero ```
How to extract the fractional part as int?
num := decimal.NewFromFloat(1.111111).Sub(decimal.NewFromFloat(1.222222)).RoundFloor(4) fmt.Println("1.111111 - 1.222222 =", num) 1.111111 - 1.222222 = -0.1112 but, the RoundFloor example was -0.1111
https://github.com/shopspring/decimal/issues/284 Fix their example errors