Closed kharabela closed 9 months ago
Hi @kharabela. The decimal struct is immutable, thus every method call returns a new decimal. This was done to prevent common mistakes, see more in our FAQ.
In your case, you initialized the tot
variable (zero-value of Decimal is 0
), and after that called Add
twice but, not used a return value anywhere. If you want to see proper result value you should
var tot decimal.Decimal
price, err := decimal.NewFromString("136.02")
if err != nil {
panic(err)
}
quantity := decimal.NewFromInt(3)
result := tot.Add(quantity).Add(decimal.NewFromInt(5))
fmt.Println("Result: ", result )
}
Thanks, how can I use in loop.I want to sum all values in loop.Please point me to an url if this is provided in document package main import ( "fmt" "github.com/shopspring/decimal" ) func main() { var result decimal.Decimal
for i := 1; i < 4; i++ {
final := result.Add(decimal.NewFromInt(5))
fmt.Println(final)
}
}
https://go.dev/play/p/FsJIRCO21KL
package main
import ( "fmt"
)
func main() { var tot decimal.Decimal price, err := decimal.NewFromString("136.02") if err != nil { panic(err) }
}