In Java, Character.getNumericValue(ch) returns the number that a character stands for, as an int. Digits give their value ('5' is 5), and the Latin letters a to z in either case give 10 to 35, so 'a' is 10 and 'Z' is 35.
It also knows other numeric characters from the Unicode data, such as superscript two (2) and ROMAN NUMERAL FIFTY, U+216C (50). It returns -1 when the character has no numeric value and -2 when the value is not a non-negative whole number, such as the fraction one half (U+00BD).
A static method of java.lang.Character (Java 1.1+): call it on the class, as in Character.getNumericValue(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version.
The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
Syntax
Pass a char or an int code point; there is no radix.
Overloads: getNumericValue(char ch) (Java 1.1) and getNumericValue(int codePoint) (Java 5). The letter values 10 to 35 apply to ASCII and fullwidth Latin letters, matching the digits used by radix 36.
Library declarations
Java
// java.lang.Character
public static int getNumericValue(char ch)
public static int getNumericValue(int codePoint) // since Java 5
Parameters
Parameter
Description
ch (char)
The character to read.
codePoint (int)
The code point to read.
Return Value
Return Value
Description
0 or more
The numeric value.
-1
No numeric value (for example '$' or a space).
-2
A value that is not a non-negative integer, such as a fraction.
Digits and letters.'7' is 7, and the letters follow the radix-36 scheme: 'a' is 10 and 'Z' is 35.
Unicode numbers. Arabic-Indic three (1635) is 3, superscript two (178) is 2, and ROMAN NUMERAL FIFTY (8556) is 50.
The two special results. One half (189) has a numeric value that is not a whole number, so it returns -2; the dollar sign (36) has no numeric value and returns -1.
Example 2 — Add Up the Roman Numerals One to Twelve
Unicode has single characters for the clock-face Roman numerals U+2160 to U+216B. Read their values and add them up.
Java
public class CharacterGetNumericValueRoman {
public static void main(String[] args) {
int sum = 0;
StringBuilder values = new StringBuilder();
for (char c = '\u2160'; c <= '\u216B'; c++) {
int v = Character.getNumericValue(c);
values.append(v).append(' ');
sum += v;
}
System.out.println(values.toString().trim());
System.out.println("sum = " + sum);
}
}
Output
1 2 3 4 5 6 7 8 9 10 11 12
sum = 78
How It Works
Twelve consecutive characters. ROMAN NUMERAL ONE (U+2160) through ROMAN NUMERAL TWELVE (U+216B) sit next to each other, so a char loop can visit them in order.
Values from Unicode data. Each character carries its numeric value, so U+2169 gives 10 without any parsing of I, V, or X.
The total. 1 + 2 + ... + 12 is 12 * 13 / 2 = 78, which matches the printed sum.
Example 3 — When getNumericValue and digit Disagree
Compare both methods (with radix 10) on a digit, a letter, a superscript, and a Roman numeral.
Java
public class CharacterGetNumericValueVsDigit {
public static void main(String[] args) {
char[] chars = {'8', 'a', '\u00B2', '\u2163'};
for (char c : chars) {
System.out.println((int) c + " numericValue=" + Character.getNumericValue(c)
+ " digit10=" + Character.digit(c, 10));
}
}
}
They agree on real digits. For '8' both methods return 8.
A letter is not a decimal digit.getNumericValue('a') is 10, but digit('a', 10) is -1 because a only counts as a digit for radix 11 and above.
Numbers that are not digits. Superscript two (178) and ROMAN NUMERAL FOUR (8547) have values 2 and 4, yet neither is a decimal digit, so digit rejects them.
Frequently Asked Questions
For a to z and A to Z it returns 10 to 35, the same values the letters have as digits in radix 36.
-1 means the character has no numeric value. -2 means it has one, but it is not a non-negative integer, as with fractions such as one half.
No. It returns values for letters and other numeric characters too. Use isDigit or Character.digit(c, 10) for validation.
Yes, for the dedicated Roman numeral characters such as U+2160 to U+2188. It does not parse ordinary letters such as X or V as Roman numerals; those return 33 and 31.
It uses the Unicode data built into your JDK, so newly added numeric characters are only known to newer JDKs. The characters used in these examples are stable.