I've been working on a project where I have to convert a string to a uint, to make sure some money values are matching:
total, err := strconv.ParseFloat(paymentResp.Transactions[0].Amount.Total, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err.Error())
}
if o.TotalPrice != uint(total*100) {
return ctx.JSON(http.StatusBadRequest, "Unable to verify amount paid")
}
But I've seemingly found a problem when trying to do the strconv.ParseFloat()
on a couple of numbers, then attempting to multiply them by 100 (to get the cents value).
I've created an example here: Go Playground
f, _ := strconv.ParseFloat("79.35", 64)
fmt.Println(uint(f*100)) //7934
f2, _ := strconv.ParseFloat("149.20", 64)
fmt.Println(uint(f2*100)) //14919
Is ParseFloat()
what I should be using in this scenario? If not, I'd love to hear a brief explanation on this, as I'm still a programmer in learning.