java.lang.Object
com.hedera.hashgraph.sdk.Hbar
All Implemented Interfaces:
Comparable<Hbar>

public final class Hbar extends Object implements Comparable<Hbar>
Represents a quantity of hbar.

Implemented as a wrapper class to force handling of units. Direct interfacing with Hedera accepts amounts in tinybars however the nominal unit is hbar.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Hbar
    A constant value of the maximum number of hbars.
    static final Hbar
    A constant value of the minimum number of hbars.
    static final Hbar
    A constant value of zero hbars.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Hbar(long amount)
    Constructs a new Hbar of the specified value.
    Hbar(long amount, HbarUnit unit)
    Constructs a new hbar of the specified value in the specified unit.
    Hbar(BigDecimal amount)
    Constructs a new Hbar of the specified, possibly fractional value.
    Hbar(BigDecimal amount, HbarUnit unit)
    Constructs a new hbar of the specified value in the specified unit.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    boolean
     
    static Hbar
    from(long hbars)
    Returns an Hbar whose value is equal to the specified long.
    static Hbar
    from(long amount, HbarUnit unit)
    Returns an Hbar representing the value in the given units.
    static Hbar
    Returns an Hbar whose value is equal to the specified long.
    static Hbar
    from(BigDecimal amount, HbarUnit unit)
    Returns an Hbar representing the value in the given units.
    static Hbar
    Converts the provided string into an amount of hbars.
    static Hbar
    Converts the provided string into an amount of hbars.
    static Hbar
    fromTinybars(long tinybars)
    Returns an Hbar converted from the specified number of tinybars.
    Returns the number of Hbars.
    int
     
    Returns a Hbar whose value is -this.
    to(HbarUnit unit)
    Convert this hbar value to a different unit.
     
    Convert hbar to string representation in specified units.
    long
    Convert this hbar value to Tinybars.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ZERO

      public static final Hbar ZERO
      A constant value of zero hbars.
    • MAX

      public static final Hbar MAX
      A constant value of the maximum number of hbars.
    • MIN

      public static final Hbar MIN
      A constant value of the minimum number of hbars.
  • Constructor Details

    • Hbar

      public Hbar(long amount)
      Constructs a new Hbar of the specified value.
      Parameters:
      amount - The amount of Hbar
    • Hbar

      Hbar(long amount, HbarUnit unit)
      Constructs a new hbar of the specified value in the specified unit. HbarUnit
      Parameters:
      amount - the amount
      unit - the unit for amount
    • Hbar

      public Hbar(BigDecimal amount)
      Constructs a new Hbar of the specified, possibly fractional value.

      The equivalent amount in tinybar must be an integer and fit in a long (64-bit signed integer).

      E.g., 1.23456789 is a valid amount of hbar but 0.123456789 is not.

      Parameters:
      amount - The amount of Hbar
    • Hbar

      Hbar(BigDecimal amount, HbarUnit unit)
      Constructs a new hbar of the specified value in the specified unit. HbarUnit
      Parameters:
      amount - the amount
      unit - the unit for amount
  • Method Details

    • fromString

      public static Hbar fromString(CharSequence text)
      Converts the provided string into an amount of hbars.
      Parameters:
      text - The string representing the amount of Hbar
      Returns:
      Hbar
    • fromString

      public static Hbar fromString(CharSequence text, HbarUnit unit)
      Converts the provided string into an amount of hbars.
      Parameters:
      text - The string representing the amount of set units
      unit - The unit to convert from to Hbar
      Returns:
      Hbar
    • from

      public static Hbar from(long hbars)
      Returns an Hbar whose value is equal to the specified long.
      Parameters:
      hbars - The value of Hbar
      Returns:
      Hbar
    • from

      public static Hbar from(long amount, HbarUnit unit)
      Returns an Hbar representing the value in the given units.
      Parameters:
      amount - The long representing the amount of set units
      unit - The unit to convert from to Hbar
      Returns:
      Hbar
    • from

      public static Hbar from(BigDecimal hbars)
      Returns an Hbar whose value is equal to the specified long.
      Parameters:
      hbars - The BigDecimal representing the amount of Hbar
      Returns:
      Hbar
    • from

      public static Hbar from(BigDecimal amount, HbarUnit unit)
      Returns an Hbar representing the value in the given units.
      Parameters:
      amount - The BigDecimal representing the amount of set units
      unit - The unit to convert from to Hbar
      Returns:
      Hbar
    • fromTinybars

      public static Hbar fromTinybars(long tinybars)
      Returns an Hbar converted from the specified number of tinybars.
      Parameters:
      tinybars - The long representing the amount of tinybar
      Returns:
      Hbar
    • to

      public BigDecimal to(HbarUnit unit)
      Convert this hbar value to a different unit.
      Parameters:
      unit - The unit to convert to from Hbar
      Returns:
      BigDecimal
    • toTinybars

      public long toTinybars()
      Convert this hbar value to Tinybars.
      Returns:
      long
    • getValue

      public BigDecimal getValue()
      Returns the number of Hbars.
      Returns:
      BigDecimal
    • negated

      public Hbar negated()
      Returns a Hbar whose value is -this.
      Returns:
      Hbar
    • toString

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

      public String toString(HbarUnit unit)
      Convert hbar to string representation in specified units.
      Parameters:
      unit - the desired unit
      Returns:
      the string representation
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public int compareTo(Hbar o)
      Specified by:
      compareTo in interface Comparable<Hbar>