Java字符数字(char ch, int radix)方法
字符类的 digit(char ch, int radix) 方法返回字符的数值。如果基数不在 MIN_RADIX<=radix<=MAX_RADIX 范围内,或者如果 ch 的值不是特定基数中的有效数字,则返回 1。如果至少满足以下一项,则字符是有效数字。
- 方法 digit() 对字符为真,Unicode 小数点小于指定的基数。在这种情况下,返回十进制数字值。
- 如果字符是大写拉丁字母 'A' 到 'Z' 之一并且其代码小于基数,则对于 + 'A',返回 -10,对于 ?'A',返回 +10。
- 如果字符是小写拉丁字母 'a' 到 'z' 之一,并且其代码小于基数,则对于 + 'a',返回 -10,对于 -'a',返回 +10。
注意:此方法无法处理增补字符。
用法
public static int digit(char ch, int radix)
参数
上述方法需要两个参数:
- 需要转换的字符
- 基数
返回值
digit(char ch, int radix) 方法返回由指定基数中的字符表示的数值。
例子1
import java.util.Scanner;
public class JavaCharacterDigitExample1 {
public static void main(String[] args) {
// Ask the user for the input
System.out.print("Enter the input of your choice:");
// Use scanner to get the user input
Scanner s = new Scanner(System.in);
char[] value = s.nextLine().toCharArray();
// ask for radix input
System.out.print("Enter the radix:");
// get the radix
int radix = s.nextInt();
// close the scanner object
s.close();
for (char charval:value) {
int digit = Character.digit(charval, radix);
// print the result
System.out.println("The numeric value of the character is returned"+" as:" + digit);
}
}
}
输出:
Enter the input of your choice:4 Enter the radix:6 The numeric value of the character is returned as:4
例子2
public class JavaCharacterDigitExample2 {
public static void main(String[] args) {
//Create two character_primitives char1 and char2.
char char1, char2;
// assign values to ch1, ch2
char1 = '9';
char2 = '5';
// create 2 int primitives a and b.
int a, b;
// assign numeric value of ch1, ch2 to i1, i2 using radix
a = Character.digit(char1, 4);
b = Character.digit(char2, 8);
System.out.println("The numeric value of " + char1 + " in radix 4 is:"+a);
System.out.println("The numeric value of " + char2 + " in radix 8 is:"+b);
}
}
输出:
The numeric value of 9 in radix 4 is:-1 The numeric value of 5 in radix 8 is:5
例子3
public class JavaCharacterDigitExample3 {
public static void main(String args[]) {
// Initialize the array
char a[] = { 'a', 'b', 'c', 'd' };
// Create the object obj
Character obj = new Character('a');
int digit = obj.digit(3, 6);
System.out.println("The numerical value for the alphabets:'a', 'b', 'c', 'd' is given as "+digit);
char char1 = '5';
int store = Character.digit(char1, 6);
System.out.println("The numeric value of " + char1 + " in radix 4 is:"+store);
}
}
输出:
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字符数字(int codePoint, int radix)方法
字符类的 digit(int codePoint, int radix) 方法返回字符的数值。如果基数不在 MIN_RADIX<=radix<=MAX_RADIX 范围内,或者如果 ch 的值不是特定基数中的有效数字,则返回 -1。如果至少满足以下一项,则字符是有效数字。
- 方法 digit() 对字符为真,Unicode 小数点小于指定的基数。在这种情况下,返回十进制数字值。
- 如果字符是大写拉丁字母 'A' 到 'Z' 之一,并且其代码小于基数,则对于 + 'A',返回 -10,对于 -'A',返回 +10。
- 如果字符是小写拉丁字母 'a' 到 'z' 之一,并且其代码小于基数,则对于 + 'a',返回 -10,对于 -'a',返回 +10。
用法
public static int digit(int codePoint, int radix)
参数
上述方法需要两个参数:
- codePoint 是需要转换的字符
- 基数
返回值
digit(int codePoint, int radix) 方法返回由指定索引中的字符表示的数值。
示例 4
public class JavaCharacterdigit_Example1 {
public static void main(String[] args) {
// Initialize a codepoint
int codepoint = 86;
// Initialize the radix
int radix = 20;
// Conversion of codepoint to char
char ch = (char) codepoint;
int digit = Character.digit(codepoint,radix);
System.err.println("The character" + " has a numeric value as "+ digit+" using the radix "+radix);
}
}
输出:
The character has a numeric value as -1 using the radix 20
例 5
public class JavaCharacterdigit_Example2 {
public static void main(String[] args) {
// Initialize a codepoint
int codepoint1 = 86;
int codepoint2 = 15;
// Initialize the radix
int radix1 = 69;
int radix2 = 18;
// Conversion of codepoint to char
char ch1 = (char) codepoint1;
char ch2 = (char) codepoint2;
int digit1 = Character.digit(codepoint1,radix1);
int digit2 = Character.digit(codepoint2,radix2);
System.out.println("The character" + " has a numeric value as "+ digit1+" using the radix "+radix1);
System.out.println("The character" + " has a numeric value as "+ digit2+" using the radix "+radix2);
}
}
输出:
The character has a numeric value as -1 using the radix 69 The character has a numeric value as -1 using the radix 18
相关用法
- Java Character isLetter()用法及代码示例
- Java Character isAlphabetic()用法及代码示例
- Java Character isValidCodePoint()用法及代码示例
- Java Character codePointCount()用法及代码示例
- Java Character isISOControl()用法及代码示例
- Java Character getType()用法及代码示例
- Java Character getNumericValue()用法及代码示例
- Java Character isSpace()用法及代码示例
- Java Character toTitleCase()用法及代码示例
- Java Character isMirrored()用法及代码示例
- Java Character isBmpCodePoint()用法及代码示例
- Java Character toUpperCase()用法及代码示例
- Java Character isIdentifierIgnorable()用法及代码示例
- Java Character isDigit()用法及代码示例
- Java Character toChars()用法及代码示例
- Java Character compare()用法及代码示例
- Java Character isLowerCase()用法及代码示例
- Java Character isJavaIdentifierPart()用法及代码示例
- Java Character getType(char ch)用法及代码示例
- Java Character isUpperCase()用法及代码示例
注:本文由纯净天空筛选整理自 Java Character digit() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。