Example usage for com.google.gwt.i18n.client NumberFormat getFormat

List of usage examples for com.google.gwt.i18n.client NumberFormat getFormat

Introduction

In this page you can find the example usage for com.google.gwt.i18n.client NumberFormat getFormat.

Prototype

public static NumberFormat getFormat(String pattern) 

Source Link

Document

Gets a NumberFormat instance for the default locale using the specified pattern and the default currencyCode.

Usage

From source file:com.google.gwt.examples.NumberFormatExample.java

public void onModuleLoad() {
    NumberFormat fmt = NumberFormat.getDecimalFormat();
    double value = 12345.6789;
    String formatted = fmt.format(value);
    // Prints 1,2345.6789 in the default locale
    GWT.log("Formatted string is" + formatted);

    // Turn a string back into a double
    value = NumberFormat.getDecimalFormat().parse("12345.6789");
    GWT.log("Parsed value is" + value);

    // Scientific notation
    value = 12345.6789;/*w ww.  j  av  a2s.c o m*/
    formatted = NumberFormat.getScientificFormat().format(value);
    // prints 1.2345E4 in the default locale
    GWT.log("Formatted string is" + formatted);

    // Currency
    fmt = NumberFormat.getCurrencyFormat();
    formatted = fmt.format(123456.7899);
    // prints US$123,456.79 in the default locale or $123,456.79 in the en_US
    // locale
    GWT.log("Formatted currency is" + formatted);

    // Custom format
    value = 12345.6789;
    formatted = NumberFormat.getFormat("000000.000000").format(value);
    // prints 012345.678900 in the default locale
    GWT.log("Formatted string is" + formatted);
}

From source file:com.google.gwt.maps.sample.hellomaps.client.GeocoderDemo.java

License:Apache License

private void displayLatLng(LatLng point) {
    NumberFormat fmt = NumberFormat.getFormat("#.0000000#");
    latLabel.setText(fmt.format(point.getLatitude()));
    lngLabel.setText(fmt.format(point.getLongitude()));
}

From source file:com.google.gwt.maps.testing.client.maps.ElevationMapWidget.java

License:Apache License

protected void drawInfoWindow(LatLng position, double elevation) {

    NumberFormat format = NumberFormat.getFormat("###");
    String elevationStr = format.format(elevation);
    String latlngStr = "[ " + format.format(position.getLatitude()) + ", "
            + format.format(position.getLongitude()) + " ]";
    String message = "Elevation " + elevationStr + "m @ " + latlngStr;

    HTML html = new HTML(message);
    InfoWindowOptions options = InfoWindowOptions.newInstance();
    options.setContent(html);/*  w  w w  .  ja  v  a 2 s .  c  om*/
    InfoWindow iw = InfoWindow.newInstance(options);
    iw.setPosition(position);
    iw.open(mapWidget);
}

From source file:com.google.gwt.sample.client.mystockwatcherEntryPoint.java

/**
 * Update a single row in the stock table.
 *
 * @param price Stock data for a single row.
 *///w w  w .j av a 2  s  .com
private void updateTable(StockData price) {
    // Make sure the stock is still in the stock table.
    if (!stocks.contains(price.getSymbol())) {
        return;
    }

    int row = stocks.indexOf(price.getSymbol()) + 1;

    // Format the data in the Price and Change fields.
    String priceText = NumberFormat.getFormat("#,##0.00").format(price.getPrice());
    NumberFormat changeFormat = NumberFormat.getFormat("+#,##0.00;-#,##0.00");
    String changeText = changeFormat.format(price.getChange());
    String changePercentText = changeFormat.format(price.getChangePercent());

    // Populate the Price and Change fields with new data.
    stocksFlexTable.setText(row, 1, priceText);
    Label changeWidget = (Label) stocksFlexTable.getWidget(row, 2);
    changeWidget.setText(changeText + " (" + changePercentText + "%)");

    // Change the color of text in the Change field based on its value.
    String changeStyleName = "noChange";
    if (price.getChangePercent() < -0.1f) {
        changeStyleName = "negativeChange";
    } else if (price.getChangePercent() > 0.1f) {
        changeStyleName = "positiveChange";
    }

    changeWidget.setStyleName(changeStyleName);
}

From source file:com.google.gwt.sample.client.stockwatcher.java

private void updateTable(StockPrice price) {
    int row;/*from w  w  w.  jav  a  2s .  co  m*/
    String priceText, diffText, diffPercentText;
    NumberFormat diffFormat;
    Label changeWidget;
    String changeStyleName = "noChange";

    if (stocks.contains(price.getName()) == false) {
        return;
    }

    //Format text
    row = stocks.indexOf(price.getName()) + 1;
    priceText = NumberFormat.getFormat("#,##0.00").format(price.getPrice());
    diffFormat = NumberFormat.getFormat("+#,##0.00;-#,##0.00");
    diffText = diffFormat.format(price.getDiff());
    diffPercentText = diffFormat.format(price.getDiffPercent());

    //Populate price
    stocksTable.setText(row, 1, priceText);
    changeWidget = (Label) stocksTable.getWidget(row, 2);
    changeWidget.setText(diffText + " (" + diffPercentText + "%)");

    // Change the color of text in the Change field based on its value.

    if (price.getDiffPercent() < -0.1f) {
        changeStyleName = "negativeChange";
    } else if (price.getDiffPercent() > 0.1f) {
        changeStyleName = "positiveChange";
    }
    changeWidget.setStyleName(changeStyleName);
}

From source file:com.google.gwt.sample.i18n.client.NumberFormatExampleController.java

License:Apache License

@Override
protected void doParseAndRememberPattern(String pattern) {
    activeFormat = NumberFormat.getFormat(pattern);
}

From source file:com.google.gwt.sample.Marketing.client.Marketing.java

private void updateCartTable(ShoppingCart cart) {
    Button removeProductButton = new Button(" X ");
    Button updateQtyButton = new Button("save");
    TextBox qty = new TextBox();
    Image image = new Image();
    image.setPixelSize(50, 60);//www.jav  a 2s.  c  om
    int row = cartFlexTable.getRowCount();
    final int pid = cart.getProduct().getPID();
    String modelText = cart.getProduct().getModel();
    String discountPriceText = NumberFormat.getFormat("#,##0.00").format(cart.getProduct().getDiscountPrice());
    image.setUrl("images/" + cart.getProduct().getImg());
    qty.setText(Integer.toString(cart.getQty()));

    removeProductButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            int index = ShoppingCart.indexOf(pid);
            ShoppingCart.remove(index);
            cartFlexTable.removeRow(index + 1);
            ViewCart.setText("View Cart : " + ShoppingCart.getTotalCount() + " $"
                    + NumberFormat.getFormat("#,##0.00").format(ShoppingCart.getTotalPrice()));
        }
    });

    updateQtyButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            int index = ShoppingCart.indexOf(pid);
            ShoppingCart.cart[index]
                    .setQty(Integer.parseInt(((TextBox) cartFlexTable.getWidget(index + 1, 3)).getText()));
            ViewCart.setText("View Cart : " + ShoppingCart.getTotalCount() + " $"
                    + NumberFormat.getFormat("#,##0.00").format(ShoppingCart.getTotalPrice()));
        }
    });

    cartFlexTable.setText(row, 0, modelText);
    cartFlexTable.setText(row, 1, "$" + discountPriceText);
    cartFlexTable.setWidget(row, 2, image);
    cartFlexTable.setWidget(row, 3, qty);
    cartFlexTable.setWidget(row, 4, removeProductButton);
    cartFlexTable.setWidget(row, 5, updateQtyButton);

}

From source file:com.google.gwt.sample.Marketing.client.Marketing.java

private void updateListTable(Product product) {
    Button addProductButton = new Button("Add to Shopping cart");
    Image image = new Image();
    image.setPixelSize(50, 60);/*from   w  w w. jav a 2s .  c  o m*/
    int row = productFlexTable.getRowCount();
    final String PIDText = Integer.toString(product.getPID());
    String modelText = product.getModel();
    String priceText = NumberFormat.getFormat("#,##0.00").format(product.getPrice());
    NumberFormat discountFormat = NumberFormat.getFormat("#,##0.00");
    String discountText = discountFormat.format(product.getDiscount());
    String discountPriceText = discountFormat.format(product.getDiscountPrice());
    image.setUrl("images/" + product.getImg());

    addProductButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            int index = Product.indexOf(PIDText);
            Product p = Product.productList[index];
            if (account.getUsername() == "Guest") {
                Window.alert("Please login before any purchase");
                usernameTextBox.setFocus(true);
            } else {
                ShoppingCart.add(new ShoppingCart(p, account.getUsername()));
                ViewCart.setText("View Cart : " + ShoppingCart.getTotalCount() + " $"
                        + NumberFormat.getFormat("#,##0.00").format(ShoppingCart.getTotalPrice()));
            }
        }
    });

    productFlexTable.setText(row, 0, PIDText);
    productFlexTable.setText(row, 1, modelText);
    productFlexTable.setText(row, 2, "$" + priceText);
    productFlexTable.setText(row, 3, "$" + discountPriceText + " (" + discountText + "%)");
    productFlexTable.setWidget(row, 4, image);
    productFlexTable.setWidget(row, 5, addProductButton);

}

From source file:com.google.gwt.sample.Marketing.client.Marketing.java

private void updatedeliveredListTable(order o) {
    Button cartIdButton = new Button("Id");
    Button deliverButton = new Button("Deliver");
    int row = deliveredFlexTable.getRowCount();
    final int id = o.getCartId();
    cartIdButton.setText(Integer.toString(id));
    String priceText = NumberFormat.getFormat("#,##0.00").format(o.getPayableAmount());

    addProductButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            int index = 1;//order.indexOf(id)+1;
            setDelivered(id);//  ww  w  . j  a  va2 s  . com

        }

        private void setDelivered(int id) {
            // TODO Auto-generated method stub

        }
    });

    /*deliveredFlexTable.setText(row, 0, PIDText);
    deliveredFlexTable.setText(row, 1, modelText);
    deliveredFlexTable.setText(row, 2, "$" + priceText);
    deliveredFlexTable.setText(row, 3, "$" + discountPriceText + " (" + discountText + "%)");
    deliveredFlexTable.setWidget(row,4,image);
    deliveredFlexTable.setWidget(row,5,addProductButton);*/

}

From source file:com.google.gwt.sample.showcase.client.content.i18n.CwNumberFormat.java

License:Apache License

/**
 * Update the selected pattern based on the pattern in the list.
 *//*from  w  ww .  j av a2s .c om*/
@ShowcaseSource
private void updatePattern() {
    switch (patternList.getSelectedIndex()) {
    case 0:
        activeFormat = NumberFormat.getDecimalFormat();
        patternBox.setText(activeFormat.getPattern());
        patternBox.setEnabled(false);
        break;
    case 1:
        activeFormat = NumberFormat.getCurrencyFormat();
        patternBox.setText(activeFormat.getPattern());
        patternBox.setEnabled(false);
        break;
    case 2:
        activeFormat = NumberFormat.getScientificFormat();
        patternBox.setText(activeFormat.getPattern());
        patternBox.setEnabled(false);
        break;
    case 3:
        activeFormat = NumberFormat.getPercentFormat();
        patternBox.setText(activeFormat.getPattern());
        patternBox.setEnabled(false);
        break;
    case 4:
        patternBox.setEnabled(true);
        String pattern = patternBox.getText();
        try {
            activeFormat = NumberFormat.getFormat(pattern);
        } catch (IllegalArgumentException e) {
            showErrorMessage(constants.cwNumberFormatInvalidPattern());
            return;
        }
        break;
    }

    // Update the formatted value
    updateFormattedValue();
}