Java Tutorial/I18N/Currency — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:03, 1 июня 2010

Create Currency from a Locale and get its symbol and Default Fraction Digits

import java.util.Currency;
import java.util.Locale;
class CurDemo {
  public static void main(String args[]) {
    Currency c = Currency.getInstance(Locale.US);
    System.out.println("Symbol: " + c.getSymbol());
    System.out.println("Default fractional digits: " + c.getDefaultFractionDigits());
  }
}





Get currency symbol

import java.util.Currency;
import java.util.Locale;
public class Main {
  public static void main(String[] args) {
    Currency currency = Currency.getInstance(Locale.JAPAN);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());
    currency = Currency.getInstance(Locale.UK);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());
    currency = Currency.getInstance(Locale.US);
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());
    currency = Currency.getInstance(new Locale("in", "ID"));
    System.out.println("Currency.getSymbol() = " + currency.getSymbol());
  }
}





Getting Currency Symbols for Locale.FRANCE

import java.util.Currency;
import java.util.Locale;
public class Main {
  public static void main(String[] args) {
    Currency currency = Currency.getInstance(Locale.FRANCE);
    System.out.println("France: " + currency.getSymbol());
  }
}
//France: EUR





Getting Currency Symbols for Locale.UK

import java.util.Currency;
import java.util.Locale;
public class Main {
  public static void main(String[] args) {
    Currency currency = Currency.getInstance(Locale.UK);
    System.out.println("United Kingdom: " + currency.getSymbol());
  }
}
//United Kingdom: GBP





Getting Currency Symbols for Locale.US

import java.util.Currency;
import java.util.Locale;
public class Main {
  public static void main(String[] args) {
    Currency currency = Currency.getInstance(Locale.US);
    System.out.println("United States: " + currency.getSymbol());
  }
}
//United States: US$





Load currecy locale, code and symbol to JTable

import java.util.Currency;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class ShowCurrencies extends JFrame {
  public ShowCurrencies() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final Locale[] locales = Locale.getAvailableLocales();
    TableModel model = new AbstractTableModel() {
      public int getColumnCount() {
        return 3;
      }
      public String getColumnName(int column) {
        if (column == 0)
          return "Locale";
        else if (column == 1)
          return "Currency Code";
        else
          return "Currency Symbol";
      }
      public int getRowCount() {
        return locales.length;
      }
      public Object getValueAt(int row, int col) {
        if (col == 0)
          return locales[row];
        else
          try {
            if (col == 1)
              return Currency.getInstance(locales[row]).getCurrencyCode();
            else
              return Currency.getInstance(locales[row]).getSymbol(locales[row]);
          } catch (IllegalArgumentException iae) {
            return null;
          }
      }
    };
    JTable table = new JTable(model);
    JScrollPane sp = new JScrollPane(table);
    getContentPane().add(sp);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    new ShowCurrencies();
  }
}