module ElasticAPM::Util::PrecisionValidator

@api private 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.

Public Instance Methods

validate(value, precision: 0, minimum: nil) click to toggle source
# File lib/elastic_apm/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