How to enforce float in decimal format when encoding to JSON in Go

0 views
0

I have a big.float which I’m encoding into JSON . However the JSON always end up showing the float in scientific notation rater than decimal notation. I can fix this by changing the JSON to be a string rather than a number and using float.Text(‘f’), however I would really prefer to keep the type as a number.

I was a taking a look at float.Format but I don’t believe this is suitable.

A really condensed gist of what I’m doing is below. I do a lot more modification of the value of supply before encoding it to json.

type TokenSupply struct {
   TotalSupply  *big.Float  `json:"totalSupply, omitempty"`
}
supply := Float.NewFloat(1000000)
json.NewEncoder(w).Encode(TokenSupply{supply})

This returns:

{"totalSupply":"1e+06"}