How to represent money amounts safely and exactly in Golang?

For this issue, i found something here :

My problem statement is : I need to store money values exact up to 2 decimal places.
Representing in whole cents (int64) seems to be an option. Can someone share if there is any other way to deal with money values in golang? maybe, a reliable datatype alternative.
Thanks

Money must be stored using integers. Using the float type may cause a loss of accuracy after a long run, then you will lose a few cents.

Disclaimer: I haven’t yet used Go for currency calculation. I haven’t thought hard about this problem.

What about thinking of currency like how time.Duration is implemented? Something like this: https://play.golang.org/p/Woe8FnVz6LE

Thanks for reply.
I get it yes float is not an option, esp. where bigger transactions are involved.
I was looking into some better substitute to using integer

@skillian Thanks for the lead. I’ll look into it and try to see how well it goes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.