I’m trying a few decimal calculations, in a Rails app, but am getting some weird results.
@pprice = item.price - (( item.price / 100 ) * promo.discount)
@pprice = item.price - promo.discount
Each value (item.price, promo.discount etc) is a decimal. However, when trying to calculate I get mixed results, none of them being the correct total. The results range from nil, 0.0 and -2.0 depending on whether I have .to_f included.
The first calculation is based on a percentage discount and the second on a straight money reduction.
I have performed the calculations in the view (to test) and they display correctly, but when they are moved over to the controller, I get the errors.
Any help is much appreciated.
since u r converting the values to float, ur calculation results arent precise.
see String, decimal, or float datatype for price field?