Skip to main content
Version: 6.5

Number formatting

Charba provide a number format implementation which enables language-sensitive number formatting, leveraging on INTL platform.

// creates a number format
NumberFormat numberFormat = new NumberFormat();
// formats the number, shows "13.576.453,87"
Console.log(numberFormat.format(13576453.865));

Creating

A number format can be created and consumed as following:

// creates locale
CLocale german = CLocaleBuilder.create(Language.GERMAN).build();
// creates a number format
// with default locale and
// default options
NumberFormat numberFormat = new NumberFormat();
// creates a number format
// with a locale and
// default options
NumberFormat nfGerman = new NumberFormat(german);
// creates number format options
NumberFormatOptions options = new NumberFormatOptions();
// creates a number format
// with a locale and
// specific options
NumberFormat nfGermanOptions = new NumberFormat(german, options);

Options

A number format can be configured, when created, by the set of options which can change the number formatting, together with the locale.

// creates locale
CLocale german = CLocaleBuilder.create(Language.GERMAN).build();
// creates number format options
NumberFormatOptions options = new NumberFormatOptions();
// sets the currency options
options.setStyle(Style.CURRENCY);
options.setCurrency(Currency.EURO);
options.setCurrencyDisplay(CurrencyDisplay.SYMBOL);
// creates a number format
// with a locale and
// specific options
NumberFormat nfGermanOptions = new NumberFormat(german, options);
// formats the number, shows "13.576.453,87 €"
Console.log(nfGermanOptions.format(13576453.865));

The complete options are described by following table:

NameTypeDefaultDescription
compactDisplayCompactDisplayCompactDisplay.SHORTSet the compact display when notation is set to Notation.COMPACT.
currencyCurrencynullThe currency to use in currency formatting.
currencyDisplayCurrencyDisplayCurrencyDisplay.SYMBOLHow to display the currency in currency formatting.
currencySignCurrencySignCurrencySign.STANDARDIn many locales, accounting format means to wrap the number with parentheses instead of appending a minus sign.
localeMatcherLocaleMatcherLocaleMatcher.BEST_FITThe locale matching algorithm to use.
maximumFractionDigitsintThe default for plain number formatting is the larger of minimumFractionDigits and 3.
The default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by Currency.
The default for percent formatting is the larger of minimumFractionDigits and 0.
The maximum number of fraction digits to use.
Possible values are from 0 to 20.
maximumSignificantDigitsint21The maximum number of significant digits to use.
Possible values are from 1 to 21.
minimumFractionDigitsintThe default for plain number and percent formatting is 0.
The default for currency formatting is the number of minor unit digits provided by Currency.
The minimum number of fraction digits to use.
Possible values are from 0 to 20.
minimumIntegerDigitsint1The minimum number of integer digits to use.
Possible values are from 1 to 21.
minimumSignificantDigitsint1The minimum number of significant digits to use.
Possible values are from 1 to 21.
notationNotationNotation.STANDARDThe formatting that should be displayed for the number.
numberingSystemNumberingSystemnullThe numbering system to use for number formatting.
signDisplaySignDisplaySignDisplay.AUTOWhen to display the sign for the number.
styleStyleStyle.DECIMALThe formatting style to use.
unitOfMeasureDisplayMeasureUnitDisplayMeasureUnitDisplay.SHORTThe unit formatting style to use in unit formatting.
unitsOfMeasureMeasureUnit[][]The unit to use in unit formatting.
useGroupingbooleantrueWhether to use grouping separators, such as thousands separators or thousand/lakh/crore separators.

See INTL number format documentation for the details of each option.

Using

The number format provides a set of methods to enable the number formatting.

The most important one is format, to format a double as following:

// creates a number format
NumberFormat numberFormat = new NumberFormat();
// formats the number, shows "13.576.453,87"
Console.log(numberFormat.format(13576453.865D));

Resolving options

resolveOptions method provides a object with properties reflecting the locale and number formatting options computed during initialization.

// creates a number format
NumberFormat numberFormat = new NumberFormat(CLocale.US);
// gets the resolved options
IsDefaultNumberFormatOptions resolvedOptions = numberFormat.resolveOptions();
// gets currency resolved options
resolvedOptions.getStyle();
resolvedOptions.getCurrency();
resolvedOptions.getCurrencyDisplay();

Formatting to parts

formatToParts method is useful for custom formatting of number strings. It returns a list of format part objects containing the locale-specific tokens from which it possible to build custom strings while preserving the locale-specific parts.

// creates a number format
NumberFormat numberFormat = new NumberFormat(CLocale.US);
// gets the format parts
List<FormatPart> parts = numberFormat.formatToParts(13576453.865);
// scans the list , logging them
parts.forEach((element) -> Console.log(element.getType().name()+" : "+element.getValue()));
// result to console:
// INTEGER : 13
// GROUP : ,
// INTEGER : 576
// GROUP : ,
// INTEGER : 453
// DECIMAL : .
// FRACTION : 865