Class MonetaryFormat
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.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final MonetaryFormat
Standard format for the BTC denomination.static final String
Currency code for base 1 Bitcoin.static final String
Currency code for base 1/1000 Bitcoin.static final String
Currency code for base 1 satoshi.static final String
Currency code for base 1/1000000 Bitcoin.static final MonetaryFormat
Standard format for fiat amounts.static final int
static final MonetaryFormat
Standard format for the mBTC denomination.static final MonetaryFormat
Standard format for the satoshi denomination.static final String
Currency symbol for base 1 Bitcoin.static final String
Currency symbol for base 1/1000 Bitcoin.static final String
Currency symbol for base 1 satoshi.static final String
Currency symbol for base 1/1000000 Bitcoin.static final MonetaryFormat
Standard format for the µBTC denomination. -
Constructor Summary
ConstructorDescriptionConstruct a MonetaryFormat with the default configuration.MonetaryFormat
(boolean useSymbol) Construct a MonetaryFormat with the default configuration. -
Method Summary
Modifier and TypeMethodDescriptioncode()
Get currency code that will be used for current shift.Configure currency code for given decimal separator shift.codeSeparator
(char codeSeparator) Separator between currency code and formatted value.decimalMark
(char decimalMark) Set character to use as the decimal mark.digits
(char zeroDigit) Set character range to use for representing digits.boolean
Two formats are equal if they have the same parameters.Format the given monetary value to a human-readable form.int
hashCode()
minDecimals
(int minDecimals) Set minimum number of decimals to use for formatting.negativeSign
(char negativeSign) Set character to prefix negative values.noCode()
Don't display currency code when formatting.optionalDecimals
(int... groups) Set additional groups of decimals to use after the minimum decimals, if they are useful for expressing precision.Parse a human-readable coin value to aCoin
instance.Parse a human-readable fiat value to aFiat
instance.positiveSign
(char positiveSign) Set character to prefix positive values.Postfix formatted output with currency code.Prefix formatted output by currency code.repeatOptionalDecimals
(int decimals, int repetitions) Set repeated additional groups of decimals to use after the minimum decimals, if they are useful for expressing precision.roundingMode
(RoundingMode roundingMode) Set rounding mode to use when it becomes necessary.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.withLocale
(Locale locale) Configure this instance with values from aLocale
.
-
Field Details
-
BTC
Standard format for the BTC denomination. -
MBTC
Standard format for the mBTC denomination. -
UBTC
Standard format for the µBTC denomination. -
SAT
Standard format for the satoshi denomination. -
FIAT
Standard format for fiat amounts. -
CODE_BTC
Currency code for base 1 Bitcoin.- See Also:
-
CODE_MBTC
Currency code for base 1/1000 Bitcoin.- See Also:
-
CODE_UBTC
Currency code for base 1/1000000 Bitcoin.- See Also:
-
CODE_SAT
Currency code for base 1 satoshi.- See Also:
-
SYMBOL_BTC
Currency symbol for base 1 Bitcoin.- See Also:
-
SYMBOL_MBTC
Currency symbol for base 1/1000 Bitcoin.- See Also:
-
SYMBOL_UBTC
Currency symbol for base 1/1000000 Bitcoin.- See Also:
-
SYMBOL_SAT
Currency symbol for base 1 satoshi.- See Also:
-
MAX_DECIMALS
public static final int MAX_DECIMALS- See Also:
-
-
Constructor Details
-
MonetaryFormat
public MonetaryFormat()Construct a MonetaryFormat with the default configuration. -
MonetaryFormat
public MonetaryFormat(boolean useSymbol) Construct a MonetaryFormat with the default configuration.- Parameters:
useSymbol
- use unicode symbols instead of the BTC and sat codes
-
-
Method Details
-
negativeSign
Set character to prefix negative values. -
positiveSign
Set character to prefix positive values. A zero value means no sign is used in this case. For parsing, a missing sign will always be interpreted as if the positive sign was used. -
digits
Set character range to use for representing digits. It starts with the specified character representing zero. -
decimalMark
Set character to use as the decimal mark. If the formatted value does not have any decimals, no decimal mark is used either. -
minDecimals
Set minimum number of decimals to use for formatting. If the value precision exceeds all decimals specified (including additional decimals specified byoptionalDecimals(int...)
orrepeatOptionalDecimals(int, int)
), the value will be rounded. This configuration is not relevant for parsing. -
optionalDecimals
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.- Parameters:
groups
- any number numbers of decimals, one for each group
-
repeatOptionalDecimals
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.- Parameters:
decimals
- value of the group to be repeatedrepetitions
- number of repetitions
-
shift
Set number of digits to shift the decimal separator to the right, coming from the standard BTC notation that was common pre-2014. Note this will change the currency code if enabled. -
roundingMode
Set rounding mode to use when it becomes necessary. -
noCode
Don't display currency code when formatting. This configuration is not relevant for parsing. -
code
Configure currency code for given decimal separator shift. This configuration is not relevant for parsing.- Parameters:
codeShift
- decimal separator shift, seeshift
code
- currency code
-
codeSeparator
Separator between currency code and formatted value. This configuration is not relevant for parsing. -
prefixCode
Prefix formatted output by currency code. This configuration is not relevant for parsing. -
postfixCode
Postfix formatted output with currency code. This configuration is not relevant for parsing. -
withLocale
Configure this instance with values from aLocale
. -
format
Format the given monetary value to a human-readable form. -
parse
Parse a human-readable coin value to aCoin
instance.- Throws:
NumberFormatException
- if the string cannot be parsed for some reason
-
parseFiat
Parse a human-readable fiat value to aFiat
instance.- Throws:
NumberFormatException
- if the string cannot be parsed for some reason
-
code
Get currency code that will be used for current shift. -
equals
Two formats are equal if they have the same parameters. -
hashCode
public int hashCode()
-