Class: OpenHAB::Core::Types::DecimalType
- Inherits:
-
Object
- Object
- OpenHAB::Core::Types::DecimalType
- Includes:
- NumericType
- Defined in:
- lib/openhab/core/types/decimal_type.rb
Overview
DecimalType uses a java.math.BigDecimal internally and thus can be used for integers, longs and floating point numbers alike.
Direct Known Subclasses
Instance Method Summary collapse
-
#-@ ⇒ DecimalType
Unary minus.
-
#<=>(other) ⇒ Integer?
Comparison.
-
#coerce(other) ⇒ Array<(DecimalType, DecimalType)>?
Type Coercion.
-
#initialize(value) ⇒ DecimalType
constructor
Create a new instance of DecimalType.
-
#|(other) ⇒ QuantityType
Convert DecimalType to a QuantityType.
Methods included from NumericType
#+@, #eql?, #to_d, #to_f, #to_i
Constructor Details
#initialize(value) ⇒ DecimalType
Create a new instance of DecimalType
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/openhab/core/types/decimal_type.rb', line 48 def initialize(*args) unless args.length == 1 super return end value = args.first if value.is_a?(java.math.BigDecimal) super elsif value.is_a?(BigDecimal) super(value.to_java.strip_trailing_zeros) elsif value.is_a?(DecimalType) super(value.to_big_decimal) elsif value.respond_to?(:to_d) super(value.to_d.to_java.strip_trailing_zeros) else # rubocop:disable Lint/DuplicateBranch # duplicates the Java BigDecimal branch, but that needs to go first # in order to avoid unnecessary conversions super end end |
Instance Method Details
#-@ ⇒ DecimalType
Unary minus
Negates self
130 131 132 |
# File 'lib/openhab/core/types/decimal_type.rb', line 130 def -@ self.class.new(to_big_decimal.negate) end |
#<=>(other) ⇒ Integer?
Comparison
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openhab/core/types/decimal_type.rb', line 93 def <=>(other) logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" } if other.is_a?(QuantityType) || other.is_a?(HSBType) (other <=> self)&.-@ elsif other.is_a?(self.class) compare_to(other) elsif other.respond_to?(:to_d) to_d <=> other.to_d elsif other.respond_to?(:coerce) return nil unless (lhs, rhs = other.coerce(self)) lhs <=> rhs end end |
#coerce(other) ⇒ Array<(DecimalType, DecimalType)>?
Type Coercion
Coerce object to a DecimalType
117 118 119 120 121 122 |
# File 'lib/openhab/core/types/decimal_type.rb', line 117 def coerce(other) logger.trace { "Coercing #{self} as a request from #{other.class}" } return unless other.respond_to?(:to_d) [self.class.new(other.to_d), self] end |
#|(other) ⇒ QuantityType
Convert DecimalType to a QuantityType
77 78 79 80 |
# File 'lib/openhab/core/types/decimal_type.rb', line 77 def |(other) other = org.openhab.core.types.util.UnitUtils.parse_unit(other.to_str) if other.respond_to?(:to_str) QuantityType.new(to_big_decimal, other) end |