#Handling currency in Go

33 messages · Page 1 of 1 (latest)

green zodiac
#

Hello everyone

I was wondering if there are any good libraries for handling currency in Go. I found https://github.com/Rhymond/go-money and it mostly works however oddly enough I have a specific value of 4533.40 which gets displayed as $4,533.39. Seems like a mistake in implementation. Just looking for a good robust lib.

GitHub

Go implementation of Fowler's Money pattern. Contribute to Rhymond/go-money development by creating an account on GitHub.

mystic zodiac
#

That's odd.
How do you initialise "the money"?

spark viper
#

Did you initialize from float?

thick remnant
spark viper
#

You shouldn't use decimal for currency

thick remnant
#

Why?

spark viper
#

They don't do well with rounding

#

You should use integer for the smallest piece of currency, like number of pennies

thick remnant
spark viper
#

Exactly why you shouldn't use decimals

thick remnant
thick remnant
spark viper
#

For example for Bitcoin you don't store it as a float/decimal. You store it as number of the lowest piece which is called like Satoshi or something

thick remnant
#

Decimal is not floating point

thick remnant
spark viper
#

Maybe ive misunderstood that repo

thick remnant
green zodiac
stark current
#

I misread this as concurrency haha

mystic zodiac
indigo rune
#

Decimal is good

#

Float is bad

#

Float is IEEE754 which loses precision. Decimal (depending on the impl) does not

#

The one linked above is perfectly good for money

spark viper
indigo rune
#

Oh sorry I didn't see you changed your mind 🐢

brave sand
#

@indigo rune when you say decimal is good, which go data type are you referring to? Go doesn't have a decimal data type.

mystic zodiac
brave sand
#

@mystic zodiac thanks. I wasn't aware of that package. I want to clarify, do you mean decimal number or decimal point number?

mystic zodiac
#

arbitrary precision thingy

#

A rational