class ActiveRecord::Type::Decimal
Тип десятичных чисел Active Model
Тип атрибута для десятичных чисел, высокоточного представления чисел с плавающей точкой. Он зарегистрирован под ключом :decimal.
class BagOfCoffee include ActiveModel::Attributes attribute :weight, :decimal end
Numeric экземпляры преобразуются в BigDecimal экземпляры. Любые другие объекты преобразуются с помощью метода to_d, за исключением пустых строк, которые преобразуются в nil. Если метод to_d не определен, объект преобразуется в строку с помощью to_s, которая затем преобразуется с помощью to_d.
bag = BagOfCoffee.new bag.weight = 0.01 bag.weight # => 0.1e-1 bag.weight = "0.01" bag.weight # => 0.1e-1 bag.weight = "" bag.weight # => nil bag.weight = :arbitrary bag.weight # => nil (the result of `.to_s.to_d`)
Decimal точность по умолчанию равна 18 и может быть настроена при объявлении атрибута:
class BagOfCoffee include ActiveModel::Attributes attribute :weight, :decimal, precision: 24 end
Константы
- BIGDECIMAL_PRECISION
Публичные методы экземпляра
# File activemodel/lib/active_model/type/decimal.rb, line 49 def type :decimal end
# File activemodel/lib/active_model/type/decimal.rb, line 53 def type_cast_for_schema(value) value.to_s.inspect end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.