The java.lang.Character.digit() is an inbuilt method in java which returns the numeric value of the character
ch in the specified
radix. It returns -1 if the radix is not in the range
MIN_RADIX <= radix <= MAX_RADIX or if the value of ch is not a valid digit in the specified radix. A character is a valid digit if at least one of the following is true:
- The method isDigit is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned.
- The character is one of the uppercase Latin letters āAā through āZā and its code is less than radix + āAā ā 10. In this case, ch ā āAā + 10 is returned.
- The character is one of the lowercase Latin letters āaā through āzā and its code is less than radix + āaā ā 10. In this case, ch ā āaā + 10 is returned.
- The character is one of the fullwidth uppercase Latin letters A (ā\uFF21ā) through Z (ā\uFF3Aā) and its code is less than radix + ā\uFF21ā ā 10. In this case, ch ā ā\uFF21ā + 10 is returned.
- The character is one of the fullwidth lowercase Latin letters a (ā\uFF41ā) through z (ā\uFF5Aā) and its code is less than radix + ā\uFF41ā ā 10. In this case, ch ā ā\uFF41ā + 10 is returned.
Syntax:
public static int digit(char ch, int radix)
Parameters:The function accepts two parameters which are described below:
- ch- This is a mandatory parameter which specifies the character to be converted.
- radix- This is a mandatory parameter which specifies radix.
Return value: This method returns the numeric value represented by the character in the specified radix.
Below programs demonstrates the above method:
Program 1:
Output:
Numeric value of 3 in radix 5 is 3
Numeric value of 6 in radix 15 is 6
Program 2: