Java - Character digit() Method



The Java Character digit() is used to position the numeric value of a character in the specified radix.

A radix is defined as the number of unique digits, including zero, present in a numeric system. The most common radix value is 10, representing the decimal system (0-9).

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

Following is the syntax for Java Character digit() method

public static int digit(char ch, int radix)
(or)
public static int digit(int codePoint, int radix)

Parameters

  • ch − the character to be converted

  • codePoint − the character (Unicode code point) to be converted

  • radix − the radix

Return Value

This method returns the numeric value represented by the character in the specified radix.

Example

The following example shows the usage of Java Character digit(char ch, int radix) 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 = '9'; //exceeds MAX_RADIX
      ch2 = '5';

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign numeric value of ch1, ch2 to i1, i2 using radix
      i1 = Character.digit(ch1, 2);
      i2 = Character.digit(ch2, 10);
      String str1 = "Numeric value of " + ch1 + " in radix 2 is " + i1;
      String str2 = "Numeric value of " + ch2 + " in radix 10 is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Numeric value of 9 in radix 2 is -1
Numeric value of 5 in radix 10 is 5

Example

In this example, we declare and initialize an int variable with a valid code point value and a char variable with a valid char value. The int variable is type-casted into a char value and is passed as an argument to the method; while the char value is passed as it is.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      int ch1;
	   char ch2;
      ch1 = 0x0012; //exceeds MAX_RADIX
      ch2 = '5';
      int i1, i2;
      i1 = Character.digit((char)ch1, 10);
      i2 = Character.digit(ch2, 16);

      // print i1, i2 values
      System.out.println("Numeric value of " + ch1 + " in radix 10 is " + i1);
      System.out.println("Numeric value of " + ch2 + " in radix 16 is " + i2);
   }
}

Output

On compiling and running the program, the output will be achieved as −

Numeric value of 18 in radix 10 is -1
Numeric value of 5 in radix 16 is 5

Example

The following example shows the usage of Java Character digit(int codePoint, int radix) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x0034;
      cp2 = 0x004a;

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign numeric value of cp1, cp2 to i1, i2 using radix
      i1 = Character.digit(cp1, 8);
      i2 = Character.digit(cp2, 8);
      String str1 = "Numeric value of cp1 in radix 8 is " + i1;
      String str2 = "Numeric value of cp2 in radix 8 is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Numeric value of cp1 in radix 8 is 4
Numeric value of cp2 in radix 8 is -1

Example

In this example, we declare and initialize a char value and pass it as an argument to the method after typecasting it.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch1; 
      int ch2;
      ch1 = '8';
      ch2 = 0x0064;
      int i1, i2;
      i1 = Character.digit((int)ch1, 10);
      i2 = Character.digit(ch2, 16);

      // print i1, i2 values
      System.out.println("Numeric value of " + ch1 + " in radix 10 is " + i1);
      System.out.println("Numeric value of " + ch2 + " in radix 16 is " + i2);
   }
}

Output

The output to the above code is displayed as −

Numeric value of 8 in radix 10 is 8
Numeric value of 100 in radix 16 is 13
java_lang_character.htm
Advertisements