NumberFormat
An instance of NumberFormat should be used to parse (and eventually format) numbers in a localized manner:
var locale = Locale.of("de", "DE"); // Locale.FRANCE, Locale.UK, Locale.ROOT, ...
var format = NumberFormat.getIntegerInstance(locale); // getIntegerInstance() for default
int i = format.parse("1234").intValue(); // parse() returns a Number instance (Long)
Localized
That means that Locale specific decimal point, thousands-separator, and even digits will be parsed and used for formatting.
For integer parsing, as asked:
- thousands-separators are ignored;
- decimal point and decimal values are discarded for integer formatters, but used by the other formatters (e.g.
getNumberInstance(),getPercentInstance(), ...) - Unicode digits are accepted
Example:
int i;
NumberFormat format;
format = NumberFormat.getIntegerInstance(Locale.ROOT);
i = format.parse("123,456.789").intValue(); // i = 123456
i = format.parse("١٢٣٤").intValue(); // i = 1234
format = NumberFormat.getIntegerInstance(Locale.GERMANY);
i = format.parse("123.456,789").intValue(); // i = 123456 - note: changed separators
i = format.parse("١٢٣٤").intValue(); // i = 1234
Formatting with NumberFormat will use the digits as specified by the given Locale (if supported):
var locale = Locale.forLanguageTag("jv-ID-u-nu-java");
var formatter = NumberFormat.getIntegerInstance(locale);
var str = formatter.format(123456); // "꧑꧒꧓,꧔꧕꧖"
locale = Locale.forLanguageTag("th-TH-u-nu-thai");
// = new Locale.Builder().setUnicodeLocaleKeyword("nu", "thai").build(); // alternative
formatter = NumberFormat.getIntegerInstance(locale);
str = formatter.format(123456); // "๑๒๓,๔๕๖"
Leniency
NumberFormat, by default, is set to parse leniently. This will hide eventual errors in the input string. Use setStrict(true) to change this behaviour if needed.
var format = NumberFormat.getIntegerInstance(Locale.ROOT);
format.parse("123xyz456"); // just 123
format.setStrict(true);
format.parse("123xyz456"); // throws ParseException
Please refer to the documentation of NumberFormat, Locale and related classes for more details.
NumberFormat
An instance of NumberFormat should be used to parse (and eventually format) numbers in a localized manner:
var locale = Locale.of("de", "DE"); // Locale.FRANCE, Locale.UK, Locale.ROOT, ...
var format = NumberFormat.getIntegerInstance(locale); // getIntegerInstance() for default
int i = format.parse("1234").intValue(); // parse() returns a Number instance (Long)
Localized
That means that Locale specific decimal point, thousands-separator, and even digits will be parsed and used for formatting.
For integer parsing, as asked:
- thousands-separators are ignored;
- decimal point and decimal values are discarded for integer formatters, but used by the other formatters (e.g.
getNumberInstance(),getPercentInstance(), ...) - Unicode digits are accepted
Example:
int i;
NumberFormat format;
format = NumberFormat.getIntegerInstance(Locale.ROOT);
i = format.parse("123,456.789").intValue(); // i = 123456
i = format.parse("١٢٣٤").intValue(); // i = 1234
format = NumberFormat.getIntegerInstance(Locale.GERMANY);
i = format.parse("123.456,789").intValue(); // i = 123456 - note: changed separators
i = format.parse("١٢٣٤").intValue(); // i = 1234
Formatting with NumberFormat will use the digits as specified by the given Locale (if supported):
var locale = Locale.forLanguageTag("jv-ID-u-nu-java");
var formatter = NumberFormat.getIntegerInstance(locale);
var str = formatter.format(123456); // "꧑꧒꧓,꧔꧕꧖"
locale = Locale.forLanguageTag("th-TH-u-nu-thai");
// = new Locale.Builder().setUnicodeLocaleKeyword("nu", "thai").build(); // alternative
formatter = NumberFormat.getIntegerInstance(locale);
str = formatter.format(123456); // "๑๒๓,๔๕๖"
Please refer to the documentation of NumberFormat, Locale and related classes for more details.
NumberFormat
An instance of NumberFormat should be used to parse (and eventually format) numbers in a localized manner:
var locale = Locale.of("de", "DE"); // Locale.FRANCE, Locale.UK, Locale.ROOT, ...
var format = NumberFormat.getIntegerInstance(locale); // getIntegerInstance() for default
int i = format.parse("1234").intValue(); // parse() returns a Number instance (Long)
Localized
That means that Locale specific decimal point, thousands-separator, and even digits will be parsed and used for formatting.
For integer parsing, as asked:
- thousands-separators are ignored;
- decimal point and decimal values are discarded for integer formatters, but used by the other formatters (e.g.
getNumberInstance(),getPercentInstance(), ...) - Unicode digits are accepted
Example:
int i;
NumberFormat format;
format = NumberFormat.getIntegerInstance(Locale.ROOT);
i = format.parse("123,456.789").intValue(); // i = 123456
i = format.parse("١٢٣٤").intValue(); // i = 1234
format = NumberFormat.getIntegerInstance(Locale.GERMANY);
i = format.parse("123.456,789").intValue(); // i = 123456 - note: changed separators
i = format.parse("١٢٣٤").intValue(); // i = 1234
Formatting with NumberFormat will use the digits as specified by the given Locale (if supported):
var locale = Locale.forLanguageTag("jv-ID-u-nu-java");
var formatter = NumberFormat.getIntegerInstance(locale);
var str = formatter.format(123456); // "꧑꧒꧓,꧔꧕꧖"
locale = Locale.forLanguageTag("th-TH-u-nu-thai");
// = new Locale.Builder().setUnicodeLocaleKeyword("nu", "thai").build(); // alternative
formatter = NumberFormat.getIntegerInstance(locale);
str = formatter.format(123456); // "๑๒๓,๔๕๖"
Leniency
NumberFormat, by default, is set to parse leniently. This will hide eventual errors in the input string. Use setStrict(true) to change this behaviour if needed.
var format = NumberFormat.getIntegerInstance(Locale.ROOT);
format.parse("123xyz456"); // just 123
format.setStrict(true);
format.parse("123xyz456"); // throws ParseException
Please refer to the documentation of NumberFormat, Locale and related classes for more details.
NumberFormat
An instance of NumberFormat should be used to parse (and eventually format) numbers in a localized manner:
var locale = Locale.of("de", "DE"); // Locale.FRANCE, Locale.UK, Locale.ROOT, ...
var format = NumberFormat.getIntegerInstance(locale); // getIntegerInstance() for default
int i = format.parse("1234").intValue(); // parse() returns a Number instance (Long)
Localized
That means that Locale specific decimal point, thousands-separator, and even digits will be parsed and used for formatting.
For integer parsing, as asked:
- thousands-separators are ignored;
- decimal point and decimal values are discarded for integer formatters, but used by the other formatters (e.g.
getNumberInstance(),getPercentInstance(), ...) - Unicode digits are accepted
Example:
int i;
NumberFormat format;
format = NumberFormat.getIntegerInstance(Locale.ROOT);
i = format.parse("123,456.789").intValue(); // i = 123456
i = format.parse("١٢٣٤").intValue(); // i = 1234
format = NumberFormat.getIntegerInstance(Locale.GERMANY);
i = format.parse("123.456,789").intValue(); // i = 123456 - note: changed separators
i = format.parse("١٢٣٤").intValue(); // i = 1234
Formatting with NumberFormat will use the digits as specified by the given Locale (if supported):
var locale = Locale.forLanguageTag("jv-ID-u-nu-java");
var formatter = NumberFormat.getIntegerInstance(locale);
var str = formatter.format(123456); // "꧑꧒꧓,꧔꧕꧖"
locale = Locale.forLanguageTag("th-TH-u-nu-thai");
// = new Locale.Builder().setUnicodeLocaleKeyword("nu", "thai").build(); // alternative
formatter = NumberFormat.getIntegerInstance(locale);
str = formatter.format(123456); // "๑๒๓,๔๕๖"
Please refer to the documentation of NumberFormat, Locale and related classes for more details.