Class DecimalType

java.lang.Object
java.lang.Number
org.openhab.core.library.types.DecimalType
All Implemented Interfaces:
Serializable, Comparable<DecimalType>, Command, PrimitiveType, State, Type
Direct Known Subclasses:
PercentType

@NonNullByDefault public class DecimalType extends Number implements PrimitiveType, State, Command, Comparable<DecimalType>
The decimal type uses a BigDecimal internally and thus can be used for integers, longs and floating point numbers alike.
Author:
Kai Kreuzer - Initial contribution
See Also:
  • Field Details

    • BIG_DECIMAL_HUNDRED

      protected static final BigDecimal BIG_DECIMAL_HUNDRED
    • ZERO

      public static final DecimalType ZERO
    • value

      protected BigDecimal value
  • Constructor Details

    • DecimalType

      public DecimalType()
    • DecimalType

      public DecimalType(Number value)
      Creates a new DecimalType with the given value.
      Parameters:
      value - a number
    • DecimalType

      public DecimalType(String value)
      Creates a new DecimalType with the given value. The English locale is used to determine (decimal/grouping) separator characters.
      Parameters:
      value - the value representing a number
      Throws:
      NumberFormatException - when the number could not be parsed to a BigDecimal
    • DecimalType

      public DecimalType(String value, Locale locale)
      Creates a new DecimalType with the given value.
      Parameters:
      value - the value representing a number
      locale - the locale used to determine (decimal/grouping) separator characters
      Throws:
      NumberFormatException - when the number could not be parsed to a BigDecimal
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toFullString

      public String toFullString()
      Description copied from interface: Type
      Get a string representation that contains the whole internal representation of the type.

      The returned string could be consumed by the static 'valueOf(String)' method of the respective type to build a new type that is equal to this type.

      Specified by:
      toFullString in interface Type
      Returns:
      a full string representation of the type to be consumed by 'valueOf(String)'
    • valueOf

      public static DecimalType valueOf(String value)
      Static access to DecimalType(String).
      Parameters:
      value - the non null value representing a number
      Returns:
      a new DecimalType
      Throws:
      NumberFormatException - when the number could not be parsed to a BigDecimal
    • format

      public String format(String pattern)
      Description copied from interface: Type
      Formats the value of this type according to a pattern (see Formatter).
      Specified by:
      format in interface Type
      Parameters:
      pattern - the pattern to use
      Returns:
      the formatted string
    • toBigDecimal

      public BigDecimal toBigDecimal()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(DecimalType o)
      Specified by:
      compareTo in interface Comparable<DecimalType>
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • defaultConversion

      protected <T extends State> @Nullable T defaultConversion(@Nullable Class<T> target)
    • as

      public <T extends State> @Nullable T as(@Nullable Class<T> target)
      Description copied from interface: State
      Convert this State's value into another type
      Specified by:
      as in interface State
      Parameters:
      target - the desired State type
      Returns:
      the State's value in the given type's representation, or null if the conversion was not possible