Example usage for java.text NumberFormat getCurrencyInstance

List of usage examples for java.text NumberFormat getCurrencyInstance

Introduction

In this page you can find the example usage for java.text NumberFormat getCurrencyInstance.

Prototype

public static final NumberFormat getCurrencyInstance() 

Source Link

Document

Returns a currency format for the current default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:org.codekaizen.vtj.text.BpNumberFormatTest.java

/**
 * DOCUMENT ME!/*from  w w  w  .j  av a  2  s.  c o m*/
 */
@Test
public void testFormatting() {
    NumberFormat fmt1 = null;
    NumberFormat fmt2 = null;
    String s1 = null;
    String s2 = null;
    Number num1 = null;

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_NUMBER, null);
    fmt2 = NumberFormat.getNumberInstance();
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_CURRENCY, null);
    fmt2 = NumberFormat.getCurrencyInstance();
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_CURRENCY, Locale.CANADA);
    fmt2 = NumberFormat.getCurrencyInstance(Locale.CANADA);
    num1 = new Integer(25436);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    num1 = new Double(1228.744);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

    fmt1 = new BpNumberFormat(BpNumberFormat.JVM_PERCENT, null);
    fmt2 = NumberFormat.getPercentInstance();
    num1 = new Double(0.7444);
    s1 = fmt1.format(num1);
    s2 = fmt2.format(num1);
    assertEquals(s2, s1);

}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.control.ContinuousMarketEngine.java

/**
 * Creates a new instance of ContinuousMarketEngine 
 *///from   ww  w . j  av a  2 s.  c  om
public ContinuousMarketEngine(DBWriter db) {
    log.info("The Continuous Market Engine has been instantiated");

    dbw = db;
    formatter = NumberFormat.getCurrencyInstance();
    rand = new Random();
    marketStates = new Hashtable();

    log.info("Continuous Market Engine has finished initialization procedures");
}

From source file:io.stallion.utils.GeneralUtils.java

public static String formatCurrency(Double amt) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    return formatter.format(amt);
}

From source file:io.stallion.utils.GeneralUtils.java

public static String formatCurrency(Float amt) {
    NumberFormat formatter = NumberFormat.getCurrencyInstance();
    return formatter.format(amt);
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.control.CallMarketEngine.java

/**
 * Creates a new instance of ContinuousMarketEngine
 *//*www .j  av  a 2  s . c  o m*/
public CallMarketEngine(DBWriter dbw) {
    log.info("The CallMarketEngine has been instantiated");

    this.dbw = dbw;
    formatter = NumberFormat.getCurrencyInstance();
    rand = new Random();
    marketStates = new Hashtable();

    log.info("CallMarketEngine has finished initialization procedures");
}

From source file:org.openvpms.web.resource.i18n.format.NumberFormatter.java

/**
 * Returns the currency format.//w  ww.j ava  2  s . c  o m
 *
 * @return the currency format
 */
public static NumberFormat getCurrencyFormat() {
    // TODO - should use the configured currency's format, rather than the default Locale's.
    // Doesn't appear to be a simple way of going from Currency -> NumberFormat
    return NumberFormat.getCurrencyInstance();
}

From source file:com.dabay6.android.apps.carlog.ui.statistics.fragments.TotalsFragment.java

/**
 * {@inheritDoc}//  w ww. j av a2s  .  co  m
 */
@Override
public void onLoadFinished(final Loader<Cursor> cursorLoader, final Cursor cursor) {
    final DetailsAdapter adapter;
    final DetailsItemList items = new DetailsItemList();

    if (DataUtils.hasData(cursor)) {
        final String[] columnNames = cursor.getColumnNames();

        for (final String columnName : columnNames) {
            if (!columnName.equalsIgnoreCase(Columns.VEHICLE_ID.getName())) {
                final DetailsItem item;
                final int index = cursor.getColumnIndex(columnName);
                final String text;
                final float value = cursor.getFloat(index);

                if (columnName.equalsIgnoreCase(Columns.TOTAL_DISTANCE.getName())) {
                    text = String.format(getString(R.string.fuel_history_miles), value);
                } else if (columnName.equalsIgnoreCase(Columns.TOTAL_FUEL.getName())) {
                    text = String.format(getString(R.string.gallons), value);
                } else {
                    final NumberFormat currency = NumberFormat.getCurrencyInstance();

                    text = currency.format(cursor.getFloat(index));
                }

                item = new DetailsItem(StringUtils.splitIntoWords(columnName), text);

                items.add(item);
            }
        }
    }

    adapter = new DetailsAdapter(getActivity(), items, layout.list_item_statistics);

    setListAdapter(adapter);

    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

From source file:FormatTest.java

public FormatTestFrame() {
    setTitle("FormatTest");
    setSize(WIDTH, HEIGHT);//from  w  w w. java2s.co m

    JPanel buttonPanel = new JPanel();
    okButton = new JButton("Ok");
    buttonPanel.add(okButton);
    add(buttonPanel, BorderLayout.SOUTH);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridLayout(0, 3));
    add(mainPanel, BorderLayout.CENTER);

    JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField.setValue(new Integer(100));
    addRow("Number:", intField);

    JFormattedTextField intField2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField2.setValue(new Integer(100));
    intField2.setFocusLostBehavior(JFormattedTextField.COMMIT);
    addRow("Number (Commit behavior):", intField2);

    JFormattedTextField intField3 = new JFormattedTextField(
            new InternationalFormatter(NumberFormat.getIntegerInstance()) {
                protected DocumentFilter getDocumentFilter() {
                    return filter;
                }

                private DocumentFilter filter = new IntFilter();
            });
    intField3.setValue(new Integer(100));
    addRow("Filtered Number", intField3);

    JFormattedTextField intField4 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    intField4.setValue(new Integer(100));
    intField4.setInputVerifier(new FormattedTextFieldVerifier());
    addRow("Verified Number:", intField4);

    JFormattedTextField currencyField = new JFormattedTextField(NumberFormat.getCurrencyInstance());
    currencyField.setValue(new Double(10));
    addRow("Currency:", currencyField);

    JFormattedTextField dateField = new JFormattedTextField(DateFormat.getDateInstance());
    dateField.setValue(new Date());
    addRow("Date (default):", dateField);

    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
    format.setLenient(false);
    JFormattedTextField dateField2 = new JFormattedTextField(format);
    dateField2.setValue(new Date());
    addRow("Date (short, not lenient):", dateField2);

    try {
        DefaultFormatter formatter = new DefaultFormatter();
        formatter.setOverwriteMode(false);
        JFormattedTextField urlField = new JFormattedTextField(formatter);
        urlField.setValue(new URL("http://java.sun.com"));
        addRow("URL:", urlField);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        MaskFormatter formatter = new MaskFormatter("###-##-####");
        formatter.setPlaceholderCharacter('0');
        JFormattedTextField ssnField = new JFormattedTextField(formatter);
        ssnField.setValue("078-05-1120");
        addRow("SSN Mask:", ssnField);
    } catch (ParseException exception) {
        exception.printStackTrace();
    }

    JFormattedTextField ipField = new JFormattedTextField(new IPAddressFormatter());
    ipField.setValue(new byte[] { (byte) 130, 65, 86, 66 });
    addRow("IP Address:", ipField);
}

From source file:org.totschnig.myexpenses.util.Utils.java

static String formatCurrency(BigDecimal amount, Currency currency) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    int fractionDigits = currency.getDefaultFractionDigits();
    nf.setCurrency(currency);/*ww w . j  a v a 2s  .c om*/
    nf.setMinimumFractionDigits(fractionDigits);
    nf.setMaximumFractionDigits(fractionDigits);
    return nf.format(amount);
}

From source file:com.dabay6.android.apps.carlog.ui.statistics.fragments.AveragesFragment.java

/**
 * {@inheritDoc}//  w  w w  . j  a  v a2  s.  c  o  m
 */
@Override
public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) {
    final DetailsAdapter adapter;
    final DetailsItemList items = new DetailsItemList();

    if (DataUtils.hasData(cursor)) {
        final String[] columnNames = cursor.getColumnNames();

        for (final String columnName : columnNames) {
            if (!columnName.equalsIgnoreCase(Columns.VEHICLE_ID.getName())) {
                final DetailsItem item;
                final int index = cursor.getColumnIndex(columnName);
                final String text;
                final float value = cursor.getFloat(index);

                if (columnName.equalsIgnoreCase(Columns.AVERAGE_FUEL_AMOUNT.getName())
                        || columnName.equalsIgnoreCase(Columns.AVERAGE_MILES_PER_GALLON.getName())
                        || columnName.equalsIgnoreCase(Columns.MAX_MILES_PER_GALLON.getName())) {
                    text = String.format(getString(R.string.gallons), value);
                } else {
                    final NumberFormat currency = NumberFormat.getCurrencyInstance();

                    text = currency.format(cursor.getFloat(index));
                }

                item = new DetailsItem(StringUtils.splitIntoWords(columnName), text);

                items.add(item);
            }
        }
    }

    adapter = new DetailsAdapter(getActivity(), items, layout.list_item_statistics);

    setListAdapter(adapter);

    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}