Module: Atatus::Util::PrecisionValidator Private
- Defined in:
- lib/atatus/util/precision_validator.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Rounds half away from zero. If ‘minimum` is provided, and the value rounds to 0 (but was not zero to begin with), use the minimum instead.
Class Method Summary collapse
Class Method Details
.validate(value, precision: 0, minimum: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/atatus/util/precision_validator.rb', line 29 def validate(value, precision: 0, minimum: nil) float = Float(value) return nil unless (0.0..1.0).cover?(float) return float if float == 0 multiplier = Float(10**precision) rounded = (float * multiplier + 0.5).floor / multiplier if rounded == 0 && minimum minimum else rounded end rescue ArgumentError nil end |