Bạn có thể sử dụng number format hoặc Currencies
Code tham khảo
static public void displayNumber(Locale currentLocale) {
Integer quantity = new Integer(123456);
Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String quantityOut;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
quantityOut = numberFormatter.format(quantity);
amountOut = numberFormatter.format(amount);
System.out.println(quantityOut + " " + currentLocale.toString());
System.out.println(amountOut + " " + currentLocale.toString());
}
Out put
123,456 vi_VN
345,987.246 vi_VN
Hoặc
static public void displayCurrency( Locale currentLocale) {
Double currencyAmount = new Double(9876543.21);
Currency currentCurrency = Currency.getInstance(currentLocale);
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance(currentLocale);
System.out.println(
currentLocale.getDisplayName() + ", " +
currentCurrency.getDisplayName() + ": " +
currencyFormatter.format(currencyAmount));
}
Out put
French (France), Euro: 9 876 543,21 €
VietNam(Vietnamese), Dong: 9.876.543,21 VND/DONG
English (United States), US Dollar: $9,876,543.21
Source
Clone() : là một cách để tạo một bản sao của một đối tượng.
Tại sao phải dùng clone(); trong nhiều trường hợp để tiết kiệm tài nguyên, thay vì sử dụng new để tạo đối tượng mới.
Dòng lệnh này. Bạn ép kiểu đối tượng this trong (KhoiAB) sang (LopA) rồi lại clone chính nó. Mình nghĩ chương trình sẽ không chạy và có lỗi compile-time error
Không ép kiểu được thôi
Với đoạn code trên thì mình sẽ không hoàn toàn hiểu được, bạn cần up toàn bộ classs A và KhoiAB có liên quan