Class: OpenHAB::Core::Types::DecimalType

Inherits:
Object
  • Object
show all
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.

Examples:

DecimalType can be used in case statements with ranges

# Check if number item is less than 50
case NumberOne.state
when (0...50)
  logger.info("#{NumberOne} is less than 50")
when (50..100)
  logger.info("#{NumberOne} is greater than 50")
end

DecimalType can be compared directly against Numeric

if NumberOne.state > 10
  logger.info("Item #{NumberOne.name} is greater than 10")
end

DecimalType can be compared against the value of another DecimalType

if NumberOne.state > NumberTwo.state
  logger.info("Item #{NumberOne} (#{NumberOne.state}) is greater than #{NumberTwo} (#{NumberTwo.state})")
end

Direct Known Subclasses

PercentType

Instance Method Summary collapse

Methods included from NumericType

#+@, #eql?, #to_d, #to_f, #to_i

Constructor Details

#initialize(value) ⇒ DecimalType

Create a new instance of DecimalType

Parameters:



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

Returns:



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

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, +1 depending on whether other is less than, equal to, or greater than self

    nil is returned if the two values are incomparable.



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

Parameters:

Returns:



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

Parameters:

Returns:



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