I have model named Dish which has an attribute cost of float type. I've set precision to 15 and scale to 2. The problem is in rails console, the value for cost is displayed in scientific notation.
cost: 0.102e2
How to display in decimals?
I have model named Dish which has an attribute cost of float type. I've set precision to 15 and scale to 2. The problem is in rails console, the value for cost is displayed in scientific notation.
cost: 0.102e2
How to display in decimals?
you can use to_f for simple format, but for more options you can also use NumberHelper module, you can format with many format from this helper module , here is reference
from your console:
include ActionView::Helpers::NumberHelper
number_with_precision(cost, precision: 2)
To prevent ActiveRecord from displaying decimal values in scientific notation, you can set the :float
column type when defining your model's attributes. This will cause ActiveRecord to display decimal values in float format rather than scientific notation.
For example, you might define your model's attributes like this:
class MyModel < ApplicationRecord
attribute :my_decimal_value, :float
end
This will cause ActiveRecord to display the my_decimal_value
attribute in float format rather than scientific notation.