Javatpoint Logo
Javatpoint Logo

Java Character digit() Method

Java Character digit(char ch, int radix) Method

The digit(char ch, int radix) method of character class returns the numeric value of the character. 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 a particular radix, 1 is returned. A character is a valid digit if atleast one of the following is true.

  • The method digit() is true for the character and the Unicode decimal is less than the specified radix. In this case, the decimal digit value is returned.
  • If the character is one of the uppercase Latin letters 'A' to 'Z' and its code is less than the radix, for + 'A', -10 is returned and for ?'A', +10 is returned.
  • If the character is one of the lowercase Latin letters 'a' to 'z' and its code is less than the radix, for + 'a', -10 is returned and for -'a', +10 is returned.

Note: This method cannot handle the supplementary character.

Syntax

Parameter

The above method requires two parameters:

  1. The character that needs to be converted
  2. The radix

Return Value

The digit(char ch, int radix) method returns the numeric value represented by the character in the specified radix.

Example 1

Test it Now

Output:

Enter the input of your choice:4
Enter the radix:6
The numeric value of the character is returned as: 4

Example 2

Test it Now

Output:

The numeric value of 9 in radix 4 is:-1
The numeric value of 5 in radix 8 is:5

Example 3

Test it Now

Output:

The numerical value for the alphabets:'a', 'b', 'c', 'd' is given as -1
The numeric value of 5 in radix 4 is: 5

Java Character digit(int codePoint, int radix) Method

The digit(int codePoint, int radix) method of character class returns the numeric value of the character. 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 a particular radix, -1 is returned. A character is a valid digit if atleast one of the following is true.

  • The method digit() is true for the character and the Unicode decimal is less than the specified radix. In this case, the decimal digit value is returned.
  • If the character is one of the uppercase Latin letters 'A' to 'Z' and its code is less than the radix, for + 'A', -10 is returned and for -'A', +10 is returned.
  • If the character is one of the lowercase Latin letters 'a' to 'z' and its code is less than the radix, for + 'a', -10 is returned and for -'a', +10 is returned.

Syntax

Parameter

The above method requires two parameters:

  • The codePoint is the character that needs to be converted
  • The radix

Return Value

The digit(int codePoint, int radix) method returns the numeric value represented by the character in the specified index.

Example 4

Test it Now

Output:

The character has a numeric value as -1 using the radix 20

Example 5

Test it Now

Output:

The character has a numeric value as -1 using the radix 69
The character has a numeric value as -1 using the radix 18
Next TopicJava Character



Please Share

facebook twitter google plus pinterest