dimanche 8 août 2021

Decimal values are truncating with to_f

I have model called Item, where I am updating the unit_price (Data Type is Decimal) value, currently I am not putting any limit when storing the value, storing the value as it is. But now I can see this PG error PG::NumericValueOutOfRange, when the value exceeds the limit.

So I was just trying to limit the value and checking something in the console, Below is the data. (Here in the data I am not putting all the decimal values)

#<Item id: 167199, description: "192830139", category_id: 10327, unit_id: 5596, weight: 0.1e5, unit_price: 0.4083333333659917816764132553606237816656920077972709552126705653021442494641325536062378168e1

i = Item.find 167199

i.unit_price.to_f
=> 4.083333333659918

#<Item id: 167199, description: "192830139", category_id: 10327, unit_id: 5596, weight: 0.1e5, unit_price: 0.6511366980197836882065909262763993442019943880913510722934069011050182329156169820243980265070876781866034494363303661586489199452739290976143216266200531728395970406461889852558384421962422689303402903e-2

i.unit_price.to_f
=> 0.006511366980197837

Can I know what will be the reason this data shows like truncated after I apply to_f ? What will be best way to solve this issue, I was just thinking about some truncate with some limit.

Aucun commentaire:

Enregistrer un commentaire