Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't understand Character.digit in Java

Tags:

java

character

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 ?

like image 227
user3497284 Avatar asked Apr 20 '14 16:04

user3497284


People also ask

What does character digit do in Java?

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.

How do you check whether a character is digit or not in Java?

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.

How do you declare a digit in Java?

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 .

Is digit or not in Java?

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.


1 Answers

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.

like image 64
Michael0x2a Avatar answered Oct 06 '22 01:10

Michael0x2a