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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。