Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:com.concursive.connect.web.modules.wiki.utils.WikiToHTMLUtils.java

public static String toHtmlFormField(CustomFormField field, WikiToHTMLContext context) {
    // Set a default value
    if (field.getValue() == null) {
        field.setValue(field.getDefaultValue());
    }//from w ww.j  a v  a  2s . co m
    // Protect against any arbitrary input
    String fieldName = StringUtils.toHtmlValue(field.getName());
    // Return output based on type
    switch (field.getType()) {
    case CustomFormField.TEXTAREA:
        String textAreaValue = StringUtils.replace(field.getValue(), "^", CRLF);
        return ("<textarea cols=\"" + field.getColumns() + "\" rows=\"" + field.getRows() + "\" name=\""
                + fieldName + "\">" + StringUtils.toString(textAreaValue) + "</textarea>");
    case CustomFormField.SELECT:
        LookupList lookupList = field.getLookupList();
        int selectedItemId = -1;
        for (LookupElement thisElement : lookupList) {
            if (field.getValue().equals(thisElement.getDescription())) {
                selectedItemId = thisElement.getCode();
            }
        }
        return lookupList.getHtmlSelect(fieldName, selectedItemId);
    case CustomFormField.CHECKBOX:
        return ("<input type=\"checkbox\" name=\"" + fieldName + "\" value=\"ON\" "
                + ("true".equals(field.getValue()) ? "checked" : "") + ">");
    case CustomFormField.CALENDAR:
        String calendarValue = field.getValue();
        if (StringUtils.hasText(calendarValue)) {
            try {
                String convertedDate = DateUtils.getUserToServerDateTimeString(null, DateFormat.SHORT,
                        DateFormat.LONG, field.getValue());
                Timestamp timestamp = DatabaseUtils.parseTimestamp(convertedDate);
                Locale locale = Locale.getDefault();
                int dateFormat = DateFormat.SHORT;
                SimpleDateFormat dateFormatter = (SimpleDateFormat) SimpleDateFormat.getDateInstance(dateFormat,
                        locale);
                calendarValue = dateFormatter.format(timestamp);
            } catch (Exception e) {
                LOG.error("toHtmlFormField calendar", e);
            }
        }
        // Output with a calendar control
        String language = System.getProperty("LANGUAGE");
        String country = System.getProperty("COUNTRY");
        return ("<input type=\"text\" name=\"" + fieldName + "\" id=\"" + fieldName + "\" size=\"10\" value=\""
                + StringUtils.toHtmlValue(calendarValue) + "\" > "
                + "<a href=\"javascript:popCalendar('inputForm', '" + fieldName + "','" + language + "','"
                + country + "');\">" + "<img src=\"" + context.getServerUrl()
                + "/images/icons/stock_form-date-field-16.gif\" "
                + "border=\"0\" align=\"absmiddle\" height=\"16\" width=\"16\"/></a>");
    case CustomFormField.PERCENT:
        return ("<input type=\"text\" name=\"" + fieldName + "\" size=\"5\" value=\""
                + StringUtils.toHtmlValue(field.getValue()) + "\"> " + "%");
    case CustomFormField.INTEGER:
        // Determine the value to display in the field
        String integerValue = StringUtils.toHtmlValue(field.getValue());
        if (StringUtils.hasText(integerValue)) {
            try {
                NumberFormat formatter = NumberFormat.getInstance();
                integerValue = formatter.format(StringUtils.getIntegerNumber(field.getValue()));
            } catch (Exception e) {
                LOG.warn("Could not format integer: " + field.getValue());
            }
        }
        return ("<input type=\"text\" name=\"" + fieldName + "\" size=\"8\" value=\"" + integerValue + "\"> ");
    case CustomFormField.FLOAT:
        // Determine the value to display in the field
        String decimalValue = StringUtils.toHtmlValue(field.getValue());
        if (StringUtils.hasText(decimalValue)) {
            try {
                NumberFormat formatter = NumberFormat.getInstance();
                decimalValue = formatter.format(StringUtils.getDoubleNumber(field.getValue()));
            } catch (Exception e) {
                LOG.warn("Could not decimal format: " + field.getValue());
            }
        }
        return ("<input type=\"text\" name=\"" + fieldName + "\" size=\"8\" value=\"" + decimalValue + "\"> ");
    case CustomFormField.CURRENCY:
        // Use a currencyCode for formatting
        String currencyCode = field.getValueCurrency();
        if (currencyCode == null) {
            currencyCode = field.getCurrency();
        }
        if (!StringUtils.hasText(currencyCode)) {
            currencyCode = "USD";
        }
        HtmlSelect currencyCodeList = HtmlSelectCurrencyCode.getSelect(fieldName + "Currency", currencyCode);
        // Determine the valut to display in the field
        String currencyValue = StringUtils.toHtmlValue(field.getValue());
        if (StringUtils.hasText(currencyValue)) {
            try {
                NumberFormat formatter = NumberFormat.getNumberInstance();
                formatter.setMaximumFractionDigits(2);
                currencyValue = formatter.format(StringUtils.getDoubleNumber(field.getValue()));
            } catch (Exception e) {
                LOG.warn("Could not currencyCode format: " + field.getValue());
            }
        }
        return (currencyCodeList.getHtml() + "<input type=\"text\" name=\"" + fieldName
                + "\" size=\"8\" value=\"" + currencyValue + "\"> ");
    case CustomFormField.EMAIL:
        return ("<input type=\"text\" " + "name=\"" + fieldName + "\" maxlength=\"255\" size=\"40\" value=\""
                + StringUtils.toHtmlValue(field.getValue()) + "\" />");
    case CustomFormField.PHONE:
        return ("<input type=\"text\" " + "name=\"" + fieldName + "\" maxlength=\"60\" size=\"20\" value=\""
                + StringUtils.toHtmlValue(field.getValue()) + "\" />");
    case CustomFormField.URL:
        String value = StringUtils.toHtmlValue(field.getValue());
        if (StringUtils.hasText(value)) {
            if (!value.contains("://")) {
                value = "http://" + field.getValue();
            }
        }
        return ("<input type=\"text\" " + "name=\"" + fieldName + "\" maxlength=\"255\" size=\"40\" value=\""
                + StringUtils.toHtmlValue(value) + "\" />");
    default:
        int maxlength = field.getMaxLength();
        int size = -1;
        if (maxlength > -1) {
            if (maxlength > 40) {
                size = 40;
            } else {
                size = maxlength;
            }
        }
        return ("<input type=\"text\" " + "name=\"" + fieldName + "\" "
                + (maxlength == -1 ? "" : "maxlength=\"" + maxlength + "\" ")
                + (size == -1 ? "" : "size=\"" + size + "\" ") + "value=\""
                + StringUtils.toHtmlValue(field.getValue()) + "\" />");
    }
}

From source file:com.prowidesoftware.swift.model.field.Field30J.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8// ww  w .j av  a 2  s  .c om
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 30J");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:com.prowidesoftware.swift.model.field.Field38J.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8/*from w w  w  .  j  a  v  a 2s.co  m*/
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 38J");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:StopWatch.java

/**
 * Return a string with a table describing all tasks performed.
 * For custom reporting, call getTaskInfo() and use the task info directly.
 *//* w ww  .  j  av  a2s. c  o m*/
public String prettyPrint() {
    StringBuffer sb = new StringBuffer(shortSummary());
    sb.append('\n');
    if (!this.keepTaskList) {
        sb.append("No task info kept");
    } else {
        TaskInfo[] tasks = getTaskInfo();
        sb.append("-----------------------------------------\n");
        sb.append("ms     %     Task name\n");
        sb.append("-----------------------------------------\n");
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMinimumIntegerDigits(5);
        nf.setGroupingUsed(false);
        NumberFormat pf = NumberFormat.getPercentInstance();
        pf.setMinimumIntegerDigits(3);
        pf.setGroupingUsed(false);
        for (int i = 0; i < tasks.length; i++) {
            sb.append(nf.format(tasks[i].getTimeMillis()) + "  ");
            sb.append(pf.format(tasks[i].getTimeSeconds() / getTotalTimeSeconds()) + "  ");
            sb.append(tasks[i].getTaskName() + "\n");
        }
    }
    return sb.toString();
}

From source file:clus.ext.hierarchical.WHTDStatistic.java

@Override
public Element getPredictElement(Document doc) {
    Element stats = doc.createElement("WHTDStat");
    NumberFormat fr = ClusFormat.SIX_AFTER_DOT;
    Attr examples = doc.createAttribute("examples");
    examples.setValue(fr.format(m_SumWeight));
    stats.setAttributeNode(examples);/*from w w  w .  j a va2s. c  o m*/
    if (m_Threshold >= 0.0) {
        String pred = computePrintTuple().toStringHuman(getHier());
        Element predictions = doc.createElement("Predictions");
        stats.appendChild(predictions);
        String[] predictionS = pred.split(",");
        for (String prediction : predictionS) {
            Element attr = doc.createElement("Prediction");
            predictions.appendChild(attr);
            attr.setTextContent(prediction);
        }
    } else {
        for (int i = 0; i < m_NbAttrs; i++) {
            Element attr = doc.createElement("Target");
            Attr name = doc.createAttribute("name");
            name.setValue(m_Attrs[i].getName());
            attr.setAttributeNode(name);
            if (m_SumWeight == 0.0) {
                attr.setTextContent("?");
            } else {
                attr.setTextContent(fr.format(getMean(i)));
            }
            stats.appendChild(attr);
        }
    }
    return stats;
}

From source file:com.prowidesoftware.swift.model.field.Field344.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//from   w ww. j  a  v  a2 s . c  om
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 344");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:com.prowidesoftware.swift.model.field.Field28E.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//from   w  w w.j  ava2s .c o  m
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 28E");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent1AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    if (component == 2) {
        //default format (as is)
        return getComponent(2);
    }
    return null;
}

From source file:com.prowidesoftware.swift.model.field.Field405.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8/*from w ww  .jav  a 2  s  .  c om*/
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 405");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.RadarChartExpression.java

private void initializeGrid(final DefaultCategoryDataset defaultDataset) {

    if (gridintervall < 0) {
        final double gridIntervalIncrement = -gridintervall;
        if ((100.0 / gridIntervalIncrement) > 5000) {
            return;
        }/* w ww.  j  a  v a 2  s. co  m*/

        //insert the gridlines (fake data sets)
        double gridline = gridIntervalIncrement;
        final int columns = defaultDataset.getColumnCount();
        final double maxdata = computeMaxValue(defaultDataset);

        final NumberFormat format = NumberFormat
                .getPercentInstance(getRuntime().getResourceBundleFactory().getLocale());
        while (gridline <= 100) {
            final double gridScaled = maxdata * gridline / 100.0;
            final String gridLineText = format.format(gridline / 100.0);
            final GridCategoryItem rowKey = new GridCategoryItem(gridLineText);
            for (int i = 0; i < columns; i++) {
                defaultDataset.addValue(gridScaled, rowKey, defaultDataset.getColumnKey(i));
            }
            gridline = gridline + gridIntervalIncrement;
        }
    } else if (gridintervall > 0) {
        final int columns = defaultDataset.getColumnCount();
        final double maxdata = computeMaxValue(defaultDataset);
        final double gridIntervalIncrement = gridintervall;
        if ((maxdata / gridIntervalIncrement) > 5000) {
            return;
        }

        final NumberFormat format = NumberFormat
                .getNumberInstance(getRuntime().getResourceBundleFactory().getLocale());
        double gridline = 0;
        while (gridline < maxdata) {
            gridline = gridline + gridIntervalIncrement;
            final String gridLineText = format.format(gridline);
            final GridCategoryItem rowKey = new GridCategoryItem(gridLineText);
            for (int i = 0; i < columns; i++) {
                defaultDataset.addValue(gridline, rowKey, defaultDataset.getColumnKey(i));
            }
        }

    }
}

From source file:com.prowidesoftware.swift.model.field.Field32F.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//from w w  w  . j  a v a2 s.  c  om
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 2) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 32F");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}