public final class MonetaryFormat extends 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 String |
CODE_BTC
Currency code for base 1 Bitcoin.
|
static String |
CODE_MBTC
Currency code for base 1/1000 Bitcoin.
|
static 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 MonetaryFormat |
UBTC
Standard format for the µBTC denomination.
|
Constructor and Description |
---|
MonetaryFormat() |
Modifier and Type | Method and Description |
---|---|
String |
code()
Get currency code that will be used for current shift.
|
MonetaryFormat |
code(int codeShift,
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.
|
CharSequence |
format(Monetary monetary)
Format the given monetary value to a human readable form.
|
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(String str)
Parse a human readable coin value to a
Coin instance. |
Fiat |
parseFiat(String currencyCode,
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(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(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 String CODE_BTC
public static final String CODE_MBTC
public static final String CODE_UBTC
public static final int MAX_DECIMALS
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(RoundingMode roundingMode)
public MonetaryFormat noCode()
public MonetaryFormat code(int codeShift, String code)
codeShift
- decimal separator shift, see shift
code
- currency codepublic MonetaryFormat codeSeparator(char codeSeparator)
public MonetaryFormat prefixCode()
public MonetaryFormat postfixCode()
public MonetaryFormat withLocale(Locale locale)
Locale
.public CharSequence format(Monetary monetary)
public Coin parse(String str) throws NumberFormatException
Coin
instance.NumberFormatException
- if the string cannot be parsed for some reasonpublic Fiat parseFiat(String currencyCode, String str) throws NumberFormatException
Fiat
instance.NumberFormatException
- if the string cannot be parsed for some reasonpublic String code()
Copyright © 2016. All rights reserved.