Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet202/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   198   199   200   201   202   203   204   205   ...   245
Bog'liq
Java 17 Recipes

CHAPTER 11
Unicode, 
Internationalization,
and Currency Codes
The Java platform provides a rich set of internationalization features to help you create 
applications that can be used across the world. The platform provides the means 
to localize your applications, format dates, and numbers in a variety of culturally 
appropriate formats and display characters used in dozens of writing systems. This 
chapter describes only some of the most frequent and common tasks that programmers 
must perform when developing internationalized applications.
11-1. Converting Unicode Characters to Digits
 Problem
You want to convert a Unicode digit character to its respective integer value. For 
example, you have a string containing the Thai digit for the value 8, and you want to 
generate an integer with that value.
 Solution
The java.lang.Character final class has several static methods to convert characters to 
integer digit values.
• public static intdigit(char ch, int radix)
• public static intdigit(intch, int radix)


398
The following code snippet iterates through the entire range of Unicode code
points from 0x0000 through 0x10FFFF. Each code point that is also a digit displays
the character and its digit value 0 through 9. You can find this example in the
org.java17recipes.chapter11.recipe11_01.Recipe11_1 class.
public static void main(String[] args) {
Recipe11_1 example = new Recipe11_1();
example.run();
}
public void run() {
int x = 0;
for (int c=0; c <= 0x10FFFF; c++) {
if (Character.isDigit(c)) {
++x;
System.out.printf("Codepoint: 0x%04X\tCharacter: %c\tDigit: 
%d\tName: %s\n", c, c,
Character.digit(c, 10), Character.getName(c));
}
}
System.out.printf("Total digits: %d\n", x);
}
}
The following is some of the output.
Codepoint: 0x0030 Character: 0 Digit: 0 Name: DIGIT ZERO
Codepoint: 0x0031 Character: 1 Digit: 1 Name: DIGIT ONE
Codepoint: 0x0032 Character: 2 Digit: 2 Name: DIGIT TWO
Codepoint: 0x0033 Character: 3 Digit: 3 Name: DIGIT THREE
Codepoint: 0x0034 Character: 4 Digit: 4 Name: DIGIT FOUR
Codepoint: 0x0035 Character: 5 Digit: 5 Name: DIGIT FIVE
Codepoint: 0x0036 Character: 6 Digit: 6 Name: DIGIT SIX
Codepoint: 0x0037 Character: 7 Digit: 7 Name: DIGIT SEVEN
Codepoint: 0x0038 Character: 8 Digit: 8 Name: DIGIT EIGHT
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


399
Codepoint: 0x0039 Character: 9 Digit: 9 Name: DIGIT NINE
Codepoint: 0x0660 Character: ٠ Digit: 0 Name: ARABIC-INDIC 
DIGIT ZERO
Codepoint: 0x0661 Character: ١ Digit: 1 Name: ARABIC-INDIC 
DIGIT ONE
Codepoint: 0x0662 Character: ٢ Digit: 2 Name: ARABIC-INDIC 
DIGIT TWO
Codepoint: 0x0663 Character: ٣ Digit: 3 Name: ARABIC-INDIC 
DIGIT THREE
Codepoint: 0x0664 Character: ٤ Digit: 4 Name: ARABIC-INDIC 
DIGIT FOUR
Codepoint: 0x0665 Character: ٥ Digit: 5 Name: ARABIC-INDIC 
DIGIT FIVE
Codepoint: 0x0666 Character: ٦ Digit: 6 Name: ARABIC-INDIC 
DIGIT SIX
Codepoint: 0x0667 Character: ٧ Digit: 7 Name: ARABIC-INDIC 
DIGIT SEVEN
Codepoint: 0x0668 Character: ٨ Digit: 8 Name: ARABIC-INDIC 
DIGIT EIGHT
Codepoint: 0x0669 Character: ٩ Digit: 9 Name: ARABIC-INDIC 
DIGIT NINE
...
Codepoint: 0x0E50 Character: 
๐ Digit: 0 Name: THAI DIGIT ZERO
Codepoint: 0x0E51 Character: 
๑ Digit: 1 Name: THAI DIGIT ONE
Codepoint: 0x0E52 Character: 
๒ Digit: 2 Name: THAI DIGIT TWO
Codepoint: 0x0E53 Character: 
๓ Digit: 3 Name: THAI DIGIT THREE
Codepoint: 0x0E54 Character: 
๔ Digit: 4 Name: THAI DIGIT FOUR
Codepoint: 0x0E55 Character: 
๕ Digit: 5 Name: THAI DIGIT FIVE
Codepoint: 0x0E56 Character: 
๖ Digit: 6 Name: THAI DIGIT SIX
Codepoint: 0x0E57 Character: 
๗ Digit: 7 Name: THAI DIGIT SEVEN
Codepoint: 0x0E58 Character: 
๘ Digit: 8 Name: THAI DIGIT EIGHT
Codepoint: 0x0E59 Character: 
๙ Digit: 9 Name: THAI DIGIT NINE
...
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


400
Note the sample code prints to the console. your console may not print all the 
character glyphs shown in this example because of font or platform differences. 
however, the characters are correctly converted to integers.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   198   199   200   201   202   203   204   205   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling