I am trying to round off the amounts i get to 2 decimal places. I have the amount attribute as a decimal field with precision 32 and scale 16.
I have tried doing amount.round(2)
def amount(amt)
amt.to_f.round(2)
end
Let's say amt = 80500.00999999999
. When printing this in rails logs, I get 80500.01
. But when saving, it saves 80500.00999999999
in database.
Also I've noted that if I do 65535.00999
(or smaller values), its rounding off and saving correctly to 65535.01. On the other hand, if am using the amount= 65536.00999
(or greater values), it saves the amount as it is in database without rounding. I took these numbers particularly since 65535
is the upper limit of unsigned smallInt
in Postgres, and somehow am getting these results.
EDIT:
I have a Transaction model which has an attribute called amount. I have another file:
# app/operations/txn.rb
def create_txn
@transaction = Transaction.new(transaction_params)
@transaction.save
end
def transaction_params
{
.
.
.
amount: dest_amount,
.
.
.
}
end
def dest_amount
#logic
end