Java Character getNumericValue() Method
Example
Convert the character '7' to the number 7, and add 1 to it:
public class Main {
public static void main(String[] args) {
char myChar = '7';
int myNum = Character.getNumericValue(myChar);
System.out.println(myNum + 1);
}
}
Definition and Usage
The getNumericValue() method returns the number that a character stands for. For example, the character '7' gives the number 7.
The letters A to Z, uppercase or lowercase, give the numbers 10 to 35. A character that has no number value, like '#', gives -1.
Note: (int) '7' does not give the same result. It gives 55, which is the number of the character in Unicode.
Syntax
One of the following:
public static int getNumericValue(char ch)
public static int getNumericValue(int codePoint)
Most programs use the first version, with a char. The second version takes the character as a code point (the number that Unicode gives each character), as an int.
Parameter Values
| Parameter | Description |
|---|---|
| ch | Required. The character to convert. |
| codePoint | Required. The code point of the character to convert. |
Technical Details
| Returns: | The number value of the character, as an int. It is -1 if the character has no number value, and -2 if the value is not a whole number of 0 or more (for example, the character for one half). |
|---|
Related Pages
❮ Character Methods