5

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?

Hariraj
  • 65
  • 8

2 Answers2

1

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)
widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • thanks, it works. Any idea how to make queries like Model.first to display in decimal format by default? – Hariraj May 03 '20 at 07:45
  • this helper module is available in your view by default, so you don't have to call it from your views – widjajayd May 03 '20 at 07:52
0

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.

Vlad Hilko
  • 1,104
  • 12
  • 17