lib/unit_measurements/math.rb in unit_measurements-4.9.0 vs lib/unit_measurements/math.rb in unit_measurements-4.10.0

- old
+ new

@@ -1,21 +1,36 @@ # -*- encoding: utf-8 -*- # -*- frozen_string_literal: true -*- # -*- warn_indent: true -*- module UnitMeasurements + # The +UnitMeasurements::Math+ mixin module provides methods for performing + # mathematical functions on the measurement. + # + # This module is included in the +Measurement+ class to allow mathematical + # functions on the measurement. + # + # @see Measurement + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 1.6.0 module Math - # Rounds quantity of the measurement. If `ndigits` is not specified, - # quantity is rounded to +Integer+. + # Rounds quantity of the measurement. If +ndigits+ is not specified, quantity + # is rounded to +Integer+. # # @example - # UnitMeasurements::Weight.new(1, "g").convert_to("st").round(4) - # => 0.0002 st + # UnitMeasurements::Length.new(17.625, "m").round + # => 18 m # - # @param [Integer] ndigits + # UnitMeasurements::Length.new(17.625, "m").round(2) + # => 17.63 m # - # @return [Measurement] + # @param [Integer, optional] ndigits The number of digits to round to. + # + # @return [Measurement] A new +Measurement+ instance with the rounded quantity. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 1.6.0 def round(ndigits = 0) self.class.new(quantity.round(ndigits), unit) end # Returns absolute value of the measurement quantity. @@ -23,32 +38,54 @@ # @example # UnitMeasurements::Length.new(-17.625, "m").abs # => 17.625 m # # @return [Measurement] + # A new +Measurement+ instance with the absolute value of the quantity. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 1.6.0 def abs self.class.new(quantity.abs, unit) end - # Rounds quantity of the measurement to next lower integer. + # Returns floored quantity of the measurement. If +ndigits+ is not specified, + # quantity is rounded to next lower +Integer+. # # @example # UnitMeasurements::Length.new(17.625, "m").floor # => 17 m # - # @return [Measurement] - def floor(*args) - self.class.new(quantity.floor(*args), unit) + # UnitMeasurements::Length.new(17.625, "m").floor(2) + # => 17.62 m + # + # @param [Integer, optional] ndigits The number of digits to round to. + # + # @return [Measurement] A new +Measurement+ instance with the floored quantity. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 1.6.0 + def floor(ndigits =0) + self.class.new(quantity.floor(ndigits), unit) end - # Rounds quantity of the measurement to next higher integer. + # Returns ceiled quantity of the measurement. If +ndigits+ is not specified, + # quantity is rounded to next higher +Integer+. # # @example # UnitMeasurements::Length.new(17.625, "m").ceil # => 18 m # - # @return [Measurement] - def ceil(*args) - self.class.new(quantity.ceil(*args), unit) + # UnitMeasurements::Length.new(17.625, "m").ceil(2) + # => 17.63 m + # + # @param [Integer, optional] ndigits The number of digits to round to. + # + # @return [Measurement] A new +Measurement+ instance with the ceiled quantity. + # + # @author {Harshal V. Ladhe}[https://shivam091.github.io/] + # @since 1.6.0 + def ceil(ndigits =0) + self.class.new(quantity.ceil(ndigits), unit) end end end