I'm not sure I understand what this method does. Can someone explain ? All I know, from what I read, is that "This method returns the numeric value represented by the character in the specified radix." I'm not sure what a radix is and what it represents. According to the description of what is being returned, I assumed this method gets a character and returns the numeric value of it in the ASCII table, but I guesses that's not true ?
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.
We can check whether the given character in a string is a number/letter by using isDigit() method of Character class. The isDigit() method is a static method and determines if the specified character is a digit.
You can do it this way: String. format("%03d", number); //number is the int. If number is 4, using the code snippet above will output: 004 .
Java Character isDigit() Method. The isDigit(char ch) method of Character class generally determines whether the given character is a digit or not. Generally, a character is considered as a digit if its general category given by Character. getType(ch), is DECIMAL_DIGIT_NUMBER.
Are you familiar with numerical bases?
For example, '3' in base 10 is equal to 3, '101' in base 2 is equal to 5 in base 10, etc.
That's essentially what Character.digit
does -- it takes a character, a specified base, and returns the numerical value in base 10.
If you provide it with a value greater then the specified base (for example, Character.digit('3', 2);
, it'll just return -1, signifying an invalid value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With