简体   繁体   中英

What's the maximum number for float on Rails validation?

I have the following validation:

class Metrics
  validates :verbs_count, allow_nil: true, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: ? }
end

I would like to know what would be the maximum value of a float without losing precision. I know it seems like a question that should be easy to find an answer, but I'm not finding it.

As per the docs (which are not in https://api.rubyonrails.org/ pages, but in the code in master):

Validates whether the value of the specified attribute is numeric by trying to convert it to a float with Kernel.Float (if only_integer is false) or applying it to the regular expression /\\A[\\+\\-]?\\d+\\z/ (if only_integer is set to true ).

Precision of Kernel.Float values are guaranteed up to 15 digits.

The answer you're looking for is maybe here: " Precision of Kernel.Float values are guaranteed up to 15 digits " - as there's not a maximum number, then you can use whatever you want, just having in mind that greather numbers will loose precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM