Closed rainerborene closed 3 years ago
Storing an integral format for currency should be done according to the number of places/precision required. This is provided by the formatter: https://github.com/ioquatix/latinum/blob/master/lib/latinum/formatters.rb#L41-L50 as this can be a lossy operation. It's totally reasonable to write "9.999999 USD" and expect multiplication by 1_000_000 to produce 9_999_999
rather than 9_990_000
which is what you'd get if you think that the smallest unit in any computation is "cents".
As an aside, one way I've solved this in the past is storing both - a string for the value that was provided by the user - and a floating point value which is used for aggregations/statistics only (e.g. total sales where a few cents or dollars won't matter on a bar chart). This is really a lot faster than using decimal types for values.
Regarding bank.parse
- this is a standard algorithm for turning strings into money instances. It's not taking into consideration the formatting options since we don't even know what formatter to use until we figured out the currency. As it stands, the amount is parsed by BigDecimal
. Therefore, the way it's currently implemented doesn't consider the delimiter/separator options you specified for formatting.
There are two (non-exclusive) options here:
bank.parse
to take into account formatting options, or#parse
method to the formatter.If we can always determine the currency by the trailing symbol, maybe this is acceptable. The key point for me is (1) predictability and (2) consistency. However, I also feel it's reasonable that Bank#parse
should be able to parse the inversion of Bank#format
which is a strong reason to do the above.
However, I also feel it's reasonable that Bank#parse should be able to parse the inversion of Bank#format which is a strong reason to do the above.
That would be awesome.
I have an open PR which should address your issue, do you mind testing it out? Maybe we can add your use case as a test/spec.
Hi @ioquatix. We're considering migrating from
money-rails
tolatinum
gem and we faced some issues during this transition. Here's what I have on mylatinum.rb
initializer. By the way, it seems thatlatinum/currencies/global
isn't required by default.First problem is about parsing values. For example:
In this example, shouldn't it return
#<Latinum::Resource "9.9 BRL">
? Second problem: In our database we're storing values asinteger
and only incents
. Is there any configuration available to handle values only incents
by default?Thanks.