money icon indicating copy to clipboard operation
money copied to clipboard

to_money not working as expected when precision is skipped?

Open rajdeep26 opened this issue 8 years ago • 5 comments

Hi Guys,

We are using money gem. Version 6.7.1

I have noticed a difference in how #to_money works with or without precision

"1.000,00".to_money("CRC").to_s
=> "1000.00"

vs

"1.000".to_money("CRC").to_s
=> "1.00"

Isn't this wrong?

The issue might be because "CRC" has a dot "." as the thousand separator and comma "," as decimal mark

NOTE: I am willing to raise a PR if this is an issue. Just let me know what to do.

rajdeep26 avatar Apr 09 '18 10:04 rajdeep26

@rajdeep26 looks like an actual issue, I'm happy to merge your PR with a fix. It seems like the issue is in the monetize gem, which provides #to_money method for the core classes.

antstorm avatar Apr 09 '18 23:04 antstorm

Thanks for confirming that. I will try to raise a PR for this.

rajdeep26 avatar Apr 10 '18 05:04 rajdeep26

@antstorm I was just going through the code and found a flag in the Monetize class called enforce_currency_delimiters.

This is what is documentation above that variable says

Monetize uses the delimiters set in the currency to separate integers from decimals, and to ignore thousands separators. In some corner cases, though, it will try to determine the correct separator by itself. Set this to true to enforce the delimiters set in the currency all the time.

I tried setting it to true and it gave correct results.

Monetize.enforce_currency_delimiters = true
=> true

"1.000".to_money("CRC").to_s
=> "1000.00"

rajdeep26 avatar Apr 10 '18 05:04 rajdeep26

@rajdeep26 - this issue appears like it has been solved and a workaround has been identified. Close this issue?

danielricecodes avatar Apr 21 '19 20:04 danielricecodes

Yes. Sure.

Thanks for all the help.

rajdeep26 avatar Apr 24 '19 09:04 rajdeep26