Serializable
, Comparable<NanoUnit>
public enum NanoUnit extends Enum<NanoUnit>
This class represents the currency units and denominations used to represent an amount of Nano, and can be used to locally convert between the different units.
If you are intending to parse or display an amount of Nano to the user, it is recommended that you use the
BASE_UNIT
constant, rather than explicitly specifying the unit. This constant represents the unit that users
of Nano will be most familiar with.
Below are a few currency conversion examples:
// Convert 1.337 knano (KILO) to the base unit (currently MEGA, or "Nano") BigDecimal conv1 = NanoUnit.BASE_UNIT .convertFrom(NanoUnit.KILO, new BigDecimal("1.337")); System.out.println("1337 knano = " + conv1.toPlainString() + " Nano"); // Prints 1337 knano = 0.001337 Nano // Convert 250 unano (MICRO) to raw (RAW) BigInteger conv2 = NanoUnit.RAW .convertFromInt(NanoUnit.MICRO, BigInteger.valueOf(250)); System.out.println("250 unano = " + conv2.toString() + " raw"); // Prints 250 unano = 250000000000000000000 raw
Enum Constant | Description |
---|---|
GIGA |
The largest divisor, equivalent to
10^33 raw. |
KILO |
The 3rd largest divisor, equivalent to
10^27 raw. |
MEGA |
The 2nd largest divisor, equivalent to
10^30 raw. |
MICRO |
The 6th largest divisor, equivalent to
10^18 raw. |
MILLI |
The 5th largest divisor, equivalent to
10^21 raw. |
RAW |
The smallest possible representable unit.
|
XRB |
The 4th largest divisor, equivalent to
10^24 raw. |
Modifier and Type | Field | Description |
---|---|---|
static NanoUnit |
BASE_UNIT |
The standard base unit currently used by most services, block explorers and exchanges.
|
Modifier and Type | Method | Description |
---|---|---|
BigDecimal |
convertFrom(NanoUnit sourceUnit,
BigDecimal sourceAmount) |
Converts the specified unit and amount into this unit.
|
BigDecimal |
convertFrom(NanoUnit sourceUnit,
BigInteger sourceAmount) |
Converts the specified unit and amount into this unit.
|
BigInteger |
convertFromInt(NanoUnit sourceUnit,
BigDecimal sourceAmount) |
Converts the specified unit and amount into this unit.
|
BigInteger |
convertFromInt(NanoUnit sourceUnit,
BigInteger sourceAmount) |
Converts the specified unit and amount into this unit.
|
String |
getClassicName() |
Returns the classic legacy name used within previous versions of the node.
|
String |
getDisplayName() |
Returns the human-readable name for this currency unit.
|
int |
getExponent() |
Returns the exponent of the unit as a power of 10.
|
BigInteger |
getRawValue() |
Returns the equivalent value of a single unit in raw.
|
static String |
toFriendlyString(BigDecimal amount,
NanoUnit sourceUnit) |
Converts a given quantity of Nano to the current base unit (
BASE_UNIT ), and formats the number to
up to 6 decimal places (rounding up truncated digits), along with a suffix of the unit name. |
static String |
toFriendlyString(BigInteger rawAmount) |
Converts a given value of raw to the current base unit (
BASE_UNIT ), and formats the number to
up to 6 decimal places (rounding up truncated digits), along with a suffix of the unit name. |
String |
toString() |
|
static NanoUnit |
valueOf(String name) |
Returns the enum constant of this type with the specified name.
|
static NanoUnit[] |
values() |
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final NanoUnit GIGA
10^33
raw.public static final NanoUnit MEGA
10^30
raw.public static final NanoUnit KILO
10^27
raw.public static final NanoUnit XRB
10^24
raw.public static final NanoUnit MILLI
10^21
raw.public static final NanoUnit MICRO
10^18
raw.public static final NanoUnit RAW
public static final NanoUnit BASE_UNIT
The standard base unit currently used by most services, block explorers and exchanges.
End-users are likely to be most familiar with this unit, and it is recommended that this constant is used so your application can be automatically updated should the units system ever change.
As of this current version, this is equal to the MEGA
unit.
public static NanoUnit[] values()
for (NanoUnit c : NanoUnit.values()) System.out.println(c);
public static NanoUnit valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic int getExponent()
Returns the exponent of the unit as a power of 10.
For instance, 10x, with x
being the value returned by this method.
public BigInteger getRawValue()
public String getDisplayName()
public String getClassicName()
public BigInteger convertFromInt(NanoUnit sourceUnit, BigInteger sourceAmount)
Converts the specified unit and amount into this unit.
If you are converting from a smaller unit and fractional digits are lost, then an ArithmeticException
will be thrown. If you wish to bypass this, use convertFrom(NanoUnit, BigInteger)
and transform
the retrieved value into a BigInteger using the BigDecimal.toBigInteger()
method.
sourceAmount
- the source amount to convert fromsourceUnit
- the source unit to convert fromArithmeticException
- if the conversion would result in a loss of informationpublic BigInteger convertFromInt(NanoUnit sourceUnit, BigDecimal sourceAmount)
Converts the specified unit and amount into this unit.
If you are converting from a smaller unit and fractional digits are lost, then an ArithmeticException
will be thrown. If you wish to bypass this, use convertFrom(NanoUnit, BigDecimal)
and transform
the retrieved value into a BigInteger using the BigDecimal.toBigInteger()
method.
sourceAmount
- the source amount to convert fromsourceUnit
- the source unit to convert fromArithmeticException
- if the conversion would result in a loss of informationpublic BigDecimal convertFrom(NanoUnit sourceUnit, BigInteger sourceAmount)
sourceAmount
- the source amount to convert fromsourceUnit
- the source unit to convert frompublic BigDecimal convertFrom(NanoUnit sourceUnit, BigDecimal sourceAmount)
sourceAmount
- the source amount to convert fromsourceUnit
- the source unit to convert frompublic static String toFriendlyString(BigInteger rawAmount)
Converts a given value of raw to the current base unit (BASE_UNIT
), and formats the number to
up to 6 decimal places (rounding up truncated digits), along with a suffix of the unit name. The value will
also be formatted to contain separating commas for every 3 digits.
For instance, a value of 1234567000000000000000000000000001
will return 1,234.567001 Nano
.
This value should not be used for any computations, and should only be used for displaying quantities of the currency to a user.
rawAmount
- the amount of raw to convert frompublic static String toFriendlyString(BigDecimal amount, NanoUnit sourceUnit)
Converts a given quantity of Nano to the current base unit (BASE_UNIT
), and formats the number to
up to 6 decimal places (rounding up truncated digits), along with a suffix of the unit name. The value will
also be formatted to contain separating commas for every 3 digits.
For instance, a value of 1234567000000000000000000000000001
RAW
will return
1,234.567001 Nano
.
This value should not be used for any computations, and should only be used for displaying quantities of the currency to a user.
amount
- the amount to convert fromsourceUnit
- the source unit of the amount to convert fromCopyright © 2020. All rights reserved.