In ruby do not use float for calculation since float is not good for precise calculation.
7 % of 200 should be 14. But float is returning
In order to ensure that calculation is right make sure that all the actors participating in calculation is of class BigDecimal . Here is how same operation can be performed using BigDecimal .
As we can see BigDecimal brings much more accurate result.
Converting money to cents
In order to charge the credit card using Stripe we needed to have the amount to be charged in cents. One way to convert the value in cents would be
Above method works but I like to delegate the functionality of making money out of a complex BigDecimal value to gem like money . In this project we are using activemerchant which depends on money gem . So we get money gem for free. You might have to add money gem to Gemfile if you want to use following technique.
money gem lets you get a money instance out of BigDecimal.
Stay in BigDecimal or money mode for calculation
If you are doing any sort of calculation then all participating elements must be either BigDecimal or Money instance. It is best if all the elements are of the same type.