Active Model Float 类型¶ ↑
用于浮点数值的属性类型。它在 :float 键下注册。
class BagOfCoffee include ActiveModel::Attributes attribute :weight, :float end bag = BagOfCoffee.new bag.weight = "0.25" bag.weight # => 0.25 bag.weight = "" bag.weight # => nil bag.weight = "NaN" bag.weight # => Float::NAN
值使用其 to_f 方法进行转换,但以下字符串除外
-
空字符串转换为
nil。 -
"Infinity"转换为Float::INFINITY。 -
"-Infinity"转换为-Float::INFINITY。 -
"NaN"转换为Float::NAN。
方法
包含的模块
实例公共方法
type() 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/float.rb, line 40 def type :float end
type_cast_for_schema(value) 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/type/float.rb, line 44 def type_cast_for_schema(value) return "::Float::NAN" if value.try(:nan?) case value when ::Float::INFINITY then "::Float::INFINITY" when -::Float::INFINITY then "-::Float::INFINITY" else super end end