Java.lang.Character.getNumericValue() Method
Description
The java.lang.Character.getNumericValue(char ch) returns the int value that the specified Unicode character represents. For example, the character '\u216C' (the roman numeral fifty) will return an int with a value of 50.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.
Declaration
Following is the declaration for java.lang.Character.getNumericValue() method
public static int getNumericValue(char ch)
Parameters
ch - the character to be converted
Return Value
This method returns the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value that is not a nonnegative integer; -1 if the character has no numeric value.
Exception
NA
Example
The following example shows the usage of lang.Character.getNumericValue() method.
package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
public static void main(String[] args) {
// create 2 character primitives ch1, ch2
char ch1, ch2;
// assign values to ch1, ch2
ch1 = 'j';
ch2 = '4';
// create 2 int primitives i1, i2
int i1, i2;
// assign numeric values of ch1, ch2 to i1, i2
i1 = Character.getNumericValue(ch1);
i2 = Character.getNumericValue(ch2);
String str1 = "Numeric value of " + ch1 + " is " + i1;
String str2 = "Numeric value of " + ch2 + " is " + i2;
// print i1, i2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
Let us compile and run the above program, this will produce the following result:
Numeric value of j is 19 Numeric value of 4 is 4