Character.digit() in Java

Approach 1: When the method takes char value and int as its parameters.

Syntax:

int java.lang.Character.digit(char ch, int radix)

This method takes two arguments, one of type char and another of type int as its parameters. This method returns the numeric value of the character ch in the specified radix. 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, -1 is returned.

Parameters: Two parameters are required for this method.

ch: the character to be converted.

radix: the radix.

Returns: the numeric value represented by the character in the specified radix.

Java

public class Characterdigitchar {
    public static void main(String[] args) {

        char ch = '2';
        int radix = 8;
        System.out.println(Character.digit(ch, radix));
    }
}

Output:

2


Approach 2: When the method takes both arguments of type int as its parameters.

Syntax:

int java.lang.Character.digit(int codePoint, int radix)

This method takes two arguments of type int as its parameters. This method returns the numeric value of the specified character (Unicode code point) in the specified radix.

Note: If the radix is not in the range MIN_RADIX ≤ radix ≤ MAX_RADIX or if the character is not a valid digit in the specified radix, -1 is returned.

Parameters: Two parameters are required for this method.

codePoint: the character (Unicode code point) to be converted.

radix: the radix.

Returns: the numeric value represented by the character in the specified radix.

Java

public class CharacterdigitcodePoint {
    public static void main(String[] args) {

        int codePoint = '#';
        int radix = 10;
        System.out.println(Character.digit(codePoint, radix));

    }
}

Output:

-1

No comments:

Post a Comment