/** * Set rounding mode to use when it becomes necessary. */ public MonetaryFormat roundingMode(RoundingMode roundingMode) { if (roundingMode == this.roundingMode) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * 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. */ public MonetaryFormat shift(int shift) { if (shift == this.shift) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Don't display currency code when formatting. This configuration is not relevant for parsing. */ public MonetaryFormat noCode() { if (codes == null) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, null, codeSeparator, codePrefixed); }
/** * Postfix formatted output with currency code. This configuration is not relevant for parsing. */ public MonetaryFormat postfixCode() { if (!codePrefixed) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, false); }
/** * Set character range to use for representing digits. It starts with the specified character representing zero. */ public MonetaryFormat digits(char zeroDigit) { if (zeroDigit == this.zeroDigit) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Prefix formatted output by currency code. This configuration is not relevant for parsing. */ public MonetaryFormat prefixCode() { if (codePrefixed) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, true); }
/** * Set minimum number of decimals to use for formatting. If the value precision exceeds all decimals specified * (including additional decimals specified by {@link #optionalDecimals(int...)} or * {@link #repeatOptionalDecimals(int, int)}), the value will be rounded. This configuration is not relevant for * parsing. */ public MonetaryFormat minDecimals(int minDecimals) { if (minDecimals == this.minDecimals) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * 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. */ public MonetaryFormat positiveSign(char positiveSign) { checkArgument(!Character.isDigit(positiveSign)); if (positiveSign == this.positiveSign) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Set character to prefix negative values. */ public MonetaryFormat negativeSign(char negativeSign) { checkArgument(!Character.isDigit(negativeSign)); checkArgument(negativeSign > 0); if (negativeSign == this.negativeSign) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Separator between currency code and formatted value. This configuration is not relevant for parsing. */ public MonetaryFormat codeSeparator(char codeSeparator) { checkArgument(!Character.isDigit(codeSeparator)); checkArgument(codeSeparator > 0); if (codeSeparator == this.codeSeparator) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Set character to use as the decimal mark. If the formatted value does not have any decimals, no decimal mark is * used either. */ public MonetaryFormat decimalMark(char decimalMark) { checkArgument(!Character.isDigit(decimalMark)); checkArgument(decimalMark > 0); if (decimalMark == this.decimalMark) return this; else return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * <p> * 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. * </p> * * <p> * For example, if you pass <tt>4,2</tt> 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. * </p> * * @param groups * any number numbers of decimals, one for each group */ public MonetaryFormat optionalDecimals(int... groups) { List<Integer> decimalGroups = new ArrayList<Integer>(groups.length); for (int group : groups) decimalGroups.add(group); return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Configure currency code for given decimal separator shift. This configuration is not relevant for parsing. * * @param codeShift * decimal separator shift * @param code * currency code */ public MonetaryFormat code(int codeShift, String code) { checkArgument(codeShift >= 0); Map<Integer, String> codes = new HashMap<Integer, String>(); if (this.codes != null) codes.putAll(this.codes); codes.put(codeShift, code); return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * <p> * 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. * </p> * * <p> * For example, if you pass <tt>1,8</tt> 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. * </p> * * @param decimals * value of the group to be repeated * @param repetitions * number of repetitions */ public MonetaryFormat repeatOptionalDecimals(int decimals, int repetitions) { checkArgument(repetitions >= 0); List<Integer> decimalGroups = new ArrayList<Integer>(repetitions); for (int i = 0; i < repetitions; i++) decimalGroups.add(decimals); return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
/** * Configure this instance with values from a {@link Locale}. */ public MonetaryFormat withLocale(Locale locale) { DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale); char negativeSign = dfs.getMinusSign(); char zeroDigit = dfs.getZeroDigit(); char decimalMark = dfs.getMonetaryDecimalSeparator(); return new MonetaryFormat(negativeSign, positiveSign, zeroDigit, decimalMark, minDecimals, decimalGroups, shift, roundingMode, codes, codeSeparator, codePrefixed); }
@Override public MonetaryFormat getPlainFormat() { if (plainFormat == null) { plainFormat = new MonetaryFormat().shift(0) .minDecimals(0).repeatOptionalDecimals(1, unitExponent).noCode(); } return plainFormat; }
@Override public MonetaryFormat getMonetaryFormat() { if (friendlyFormat == null) { friendlyFormat = new MonetaryFormat() .shift(0).minDecimals(2).code(0, symbol).postfixCode(); switch (unitExponent) { case 8: friendlyFormat = friendlyFormat.optionalDecimals(2, 2, 2); break; case 6: friendlyFormat = friendlyFormat.optionalDecimals(2, 2); break; case 4: friendlyFormat = friendlyFormat.optionalDecimals(2); break; default: friendlyFormat = friendlyFormat.minDecimals(unitExponent); } } return friendlyFormat; }