public final class MonetaryFormat
extends java.lang.Object
Utility for formatting and parsing coin values to and from human readable form.
MonetaryFormat instances are immutable. Invoking a configuration method has no effect on the receiving instance; you must store and use the new instance it returns, instead. Instances are thread safe, so they may be stored safely as static constants.
Modifier and Type | Field and Description |
---|---|
static MonetaryFormat |
BTC
Standard format for the BTC denomination.
|
static java.lang.String |
CODE_BTC
Currency code for base 1 Bitcoin.
|
static java.lang.String |
CODE_MBTC
Currency code for base 1/1000 Bitcoin.
|
static java.lang.String |
CODE_UBTC
Currency code for base 1/1000000 Bitcoin.
|
static MonetaryFormat |
FIAT
Standard format for fiat amounts.
|
static int |
MAX_DECIMALS |
static MonetaryFormat |
MBTC
Standard format for the mBTC denomination.
|
static java.lang.String |
SYMBOL_BTC
Currency symbol for base 1 Bitcoin.
|
static java.lang.String |
SYMBOL_MBTC
Currency symbol for base 1/1000 Bitcoin.
|
static java.lang.String |
SYMBOL_UBTC
Currency symbol for base 1/1000000 Bitcoin.
|
static MonetaryFormat |
UBTC
Standard format for the µBTC denomination.
|
Constructor and Description |
---|
MonetaryFormat() |
MonetaryFormat(boolean useBitcoinSymbol) |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
code()
Get currency code that will be used for current shift.
|
MonetaryFormat |
code(int codeShift,
java.lang.String code)
Configure currency code for given decimal separator shift.
|
MonetaryFormat |
codeSeparator(char codeSeparator)
Separator between currency code and formatted value.
|
MonetaryFormat |
decimalMark(char decimalMark)
Set character to use as the decimal mark.
|
MonetaryFormat |
digits(char zeroDigit)
Set character range to use for representing digits.
|
boolean |
equals(java.lang.Object o)
Two formats are equal if they have the same parameters.
|
java.lang.CharSequence |
format(Monetary monetary)
Format the given monetary value to a human readable form.
|
int |
hashCode() |
MonetaryFormat |
minDecimals(int minDecimals)
Set minimum number of decimals to use for formatting.
|
MonetaryFormat |
negativeSign(char negativeSign)
Set character to prefix negative values.
|
MonetaryFormat |
noCode()
Don't display currency code when formatting.
|
MonetaryFormat |
optionalDecimals(int... groups)
Set additional groups of decimals to use after the minimum decimals, if they are useful for expressing precision.
|
Coin |
parse(java.lang.String str)
Parse a human readable coin value to a
Coin instance. |
Fiat |
parseFiat(java.lang.String currencyCode,
java.lang.String str)
Parse a human readable fiat value to a
Fiat instance. |
MonetaryFormat |
positiveSign(char positiveSign)
Set character to prefix positive values.
|
MonetaryFormat |
postfixCode()
Postfix formatted output with currency code.
|
MonetaryFormat |
prefixCode()
Prefix formatted output by currency code.
|
MonetaryFormat |
repeatOptionalDecimals(int decimals,
int repetitions)
Set repeated additional groups of decimals to use after the minimum decimals, if they are useful for expressing
precision.
|
MonetaryFormat |
roundingMode(java.math.RoundingMode roundingMode)
Set rounding mode to use when it becomes necessary.
|
MonetaryFormat |
shift(int shift)
Set number of digits to shift the decimal separator to the right, coming from the standard BTC notation that was
common pre-2014.
|
MonetaryFormat |
withLocale(java.util.Locale locale)
Configure this instance with values from a
Locale . |
public static final MonetaryFormat BTC
public static final MonetaryFormat MBTC
public static final MonetaryFormat UBTC
public static final MonetaryFormat FIAT
public static final java.lang.String CODE_BTC
public static final java.lang.String CODE_MBTC
public static final java.lang.String CODE_UBTC
public static final java.lang.String SYMBOL_BTC
public static final java.lang.String SYMBOL_MBTC
public static final java.lang.String SYMBOL_UBTC
public static final int MAX_DECIMALS
public MonetaryFormat()
public MonetaryFormat(boolean useBitcoinSymbol)
public MonetaryFormat negativeSign(char negativeSign)
public MonetaryFormat positiveSign(char positiveSign)
public MonetaryFormat digits(char zeroDigit)
public MonetaryFormat decimalMark(char decimalMark)
public MonetaryFormat minDecimals(int minDecimals)
optionalDecimals(int...)
or
repeatOptionalDecimals(int, int)
), the value will be rounded. This configuration is not relevant for
parsing.public MonetaryFormat optionalDecimals(int... groups)
Set additional groups of decimals to use after the minimum decimals, if they are useful for expressing precision. Each value is a number of decimals in that group. If the value precision exceeds all decimals specified (including minimum decimals), the value will be rounded. This configuration is not relevant for parsing.
For example, if you pass 4,2
it will add four decimals to your formatted string if needed, and then add
another two decimals if needed. At this point, rather than adding further decimals the value will be rounded.
groups
- any number numbers of decimals, one for each grouppublic MonetaryFormat repeatOptionalDecimals(int decimals, int repetitions)
Set repeated additional groups of decimals to use after the minimum decimals, if they are useful for expressing precision. If the value precision exceeds all decimals specified (including minimum decimals), the value will be rounded. This configuration is not relevant for parsing.
For example, if you pass 1,8
it will up to eight decimals to your formatted string if needed. After
these have been used up, rather than adding further decimals the value will be rounded.
decimals
- value of the group to be repeatedrepetitions
- number of repetitionspublic MonetaryFormat shift(int shift)
public MonetaryFormat roundingMode(java.math.RoundingMode roundingMode)
public MonetaryFormat noCode()
public MonetaryFormat code(int codeShift, java.lang.String code)
codeShift
- decimal separator shift, see shift
code
- currency codepublic MonetaryFormat codeSeparator(char codeSeparator)
public MonetaryFormat prefixCode()
public MonetaryFormat postfixCode()
public MonetaryFormat withLocale(java.util.Locale locale)
Locale
.public java.lang.CharSequence format(Monetary monetary)
public Coin parse(java.lang.String str) throws java.lang.NumberFormatException
Coin
instance.java.lang.NumberFormatException
- if the string cannot be parsed for some reasonpublic Fiat parseFiat(java.lang.String currencyCode, java.lang.String str) throws java.lang.NumberFormatException
Fiat
instance.java.lang.NumberFormatException
- if the string cannot be parsed for some reasonpublic java.lang.String code()
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object