Conversation
to_amount_in_cents("30.123") returned 30123. A third decimal digit made the cent amount ten times too large.
|
Thank you for the contribution. Did you consider that there are currencies with more than 2 decimals? This solution would effectively break those, even if they are less likely to sit in camt files in the sepa area. However that is not impossible. On the other hand the method is called amount in cents, which by definition should be just multiplied by 100 Also ljust codes it to 2 decimals already. Could you share a bit more about the scenario in which this came up for you? |
|
Thanks for looking at this. The case I hit is to_amount_in_cents("30.123"). split gives cents "123", and ljust(2, "0") leaves that as-is, so the result is 30123. That is not 30.123 times 100. 30.12 still comes back as 3012, and 30.1 as 3010. You're right that the name is cents, so two decimal digits are the contract. A currency with three decimals is a real exception, and this raise would reject it. I don't have a camt file in one of those currencies. The input I was looking at was a two-decimal amount with an extra digit, which came out ten times too large. Happy to adjust if you'd rather scale by the currency's exponent, or leave three-decimal values alone. The part I wanted to stop is an extra digit being concatenated into the integer. |
|
So you used the method as a helper independent of a corresponding file? What's the background of the number having 3 decimals in the context of your usage? |
|
Yes. I called to_amount_in_cents on its own, not from a parsed camt file. 30.123 was a made-up amount so I could see what the extra digit did. I do not have a file where a three-decimal currency shows up. If you want those amounts to keep working, this raise is the wrong cut and I can drop it. |
Summary
to_amount_in_cents("30.123")returned30123. The digits after the separator are concatenated, so a third decimal makes the cent amount ten times too large. More than two digits now raises.30.12is still3012,30,12is still3012,1is still100, and30.1is still3010.Test plan
to_amount_in_cents("30.123")returns30123ArgumentError, and the conversions above still matchMade with Cursor