Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet209/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   205   206   207   208   209   210   211   212   ...   245
Bog'liq
Java 17 Recipes

\u or \U, followed by the hexadecimal representation of the Unicode code point. 
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


413
This recipe’s code sample uses the Japanese word “文字” (pronounced mo-ji). As the 
example shows, you can use the actual characters in the regular expression or look up 
the Unicode code point values. For this particular Japanese word, the encoding is
\u6587\u5B57.
The Java language’s regular expression support includes special character classes. 
For example, \d and \w are shortcut notations for the regular expressions [0-9] and 
[a-zA-Z_0-9], respectively. However, because of the Java compiler’s special handling 
of the backslash character, you must use an extra backslash when using predefined 
character classes such as \d (digits), \w (word characters), and \s (space characters). 
To use them in source code, for example, you enter 
\\d\\w, and \\s, respectively. The 
sample code used the double backslash in Solution 1 to represent the \w character class.
String enRegEx = "^The \\w+ cat.*";
11-5. Overriding the Default Currency
 Problem
You want to display a number value using a currency not associated with the 
default locale.
 Solution
Take control of which currency is printed with a formatted currency value by explicitly 
setting the currency used in a NumberFormat instance. The following example 
assumes that the default locale is Locale.JAPAN. It changes the currency by calling the 
setCurrency(Currency c) method of its NumberFormat instance. This example comes 
from the org.java17recipes.chapter11.recipe11_05.Recipe11_5 class.
public static void main(String[] args) {
Recipe11_5 app = new Recipe11_5();
app.run();
}
public void run() {
BigDecimal value = new BigDecimal(12345);
System.out.printf("Default locale: %s\n", Locale.getDefault() 
.getDisplayName());
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


414
NumberFormat nf = NumberFormat.getCurrencyInstance();
String formattedCurrency = nf.format(value);
System.out.printf("%s\n", formattedCurrency);
Currency c = Currency.getInstance(Locale.US);
nf.setCurrency(c);
formattedCurrency = nf.format(value);
System.out.printf("%s\n\n", formattedCurrency);
}
The previous code prints out the following.
Default locale: 日本語 (日本)
¥12,345
USD12,345

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   205   206   207   208   209   210   211   212   ...   245




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