Search in sources :

Example 66 with NumberFormat

use of java.text.NumberFormat in project spring-framework by spring-projects.

the class NumberStyleFormatter method getNumberFormat.

@Override
public NumberFormat getNumberFormat(Locale locale) {
    NumberFormat format = NumberFormat.getInstance(locale);
    if (!(format instanceof DecimalFormat)) {
        if (this.pattern != null) {
            throw new IllegalStateException("Cannot support pattern for non-DecimalFormat: " + format);
        }
        return format;
    }
    DecimalFormat decimalFormat = (DecimalFormat) format;
    decimalFormat.setParseBigDecimal(true);
    if (this.pattern != null) {
        decimalFormat.applyPattern(this.pattern);
    }
    return decimalFormat;
}
Also used : DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 67 with NumberFormat

use of java.text.NumberFormat in project spring-framework by spring-projects.

the class AbstractNumberFormatter method parse.

@Override
public Number parse(String text, Locale locale) throws ParseException {
    NumberFormat format = getNumberFormat(locale);
    ParsePosition position = new ParsePosition(0);
    Number number = format.parse(text, position);
    if (position.getErrorIndex() != -1) {
        throw new ParseException(text, position.getIndex());
    }
    if (!this.lenient) {
        if (text.length() != position.getIndex()) {
            // indicates a part of the string that was not parsed
            throw new ParseException(text, position.getIndex());
        }
    }
    return number;
}
Also used : ParseException(java.text.ParseException) NumberFormat(java.text.NumberFormat) ParsePosition(java.text.ParsePosition)

Example 68 with NumberFormat

use of java.text.NumberFormat in project camel by apache.

the class BindyAbstractFactory method getNumberFormat.

private static NumberFormat getNumberFormat() {
    // Get instance of NumberFormat
    NumberFormat nf = NumberFormat.getInstance();
    // set max number of digits to 3 (thousands)
    nf.setMaximumIntegerDigits(3);
    nf.setMinimumIntegerDigits(3);
    return nf;
}
Also used : NumberFormat(java.text.NumberFormat)

Example 69 with NumberFormat

use of java.text.NumberFormat in project camel by apache.

the class TimeUtils method printDuration.

/**
     * Prints the duration in a human readable format as X days Y hours Z minutes etc.
     *
     * @param uptime the uptime in millis
     * @return the time used for displaying on screen or in logs
     */
public static String printDuration(double uptime) {
    // Code taken from Karaf
    // https://svn.apache.org/repos/asf/karaf/trunk/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/InfoAction.java
    NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
    NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));
    uptime /= 1000;
    if (uptime < 60) {
        return fmtD.format(uptime) + " seconds";
    }
    uptime /= 60;
    if (uptime < 60) {
        long minutes = (long) uptime;
        String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
        return s;
    }
    uptime /= 60;
    if (uptime < 24) {
        long hours = (long) uptime;
        long minutes = (long) ((uptime - hours) * 60);
        String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
        if (minutes != 0) {
            s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
        }
        return s;
    }
    uptime /= 24;
    long days = (long) uptime;
    long hours = (long) ((uptime - days) * 24);
    String s = fmtI.format(days) + (days > 1 ? " days" : " day");
    if (hours != 0) {
        s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
    }
    return s;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 70 with NumberFormat

use of java.text.NumberFormat in project android-saripaar by ragunathjawahar.

the class AbstractNumberValidator method getFormat.

/**
     * <p>Returns a <code>NumberFormat</code> for the specified <i>pattern</i>
     *    and/or <code>Locale</code>.</p>
     *
     * @param pattern The pattern used to validate the value against or
     *        <code>null</code> to use the default for the <code>Locale</code>.
     * @param locale The locale to use for the currency format, system default if null.
     * @return The <code>NumberFormat</code> to created.
     */
protected Format getFormat(String pattern, Locale locale) {
    NumberFormat formatter = null;
    boolean usePattern = (pattern != null && pattern.length() > 0);
    if (!usePattern) {
        formatter = (NumberFormat) getFormat(locale);
    } else if (locale == null) {
        formatter = new DecimalFormat(pattern);
    } else {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
        formatter = new DecimalFormat(pattern, symbols);
    }
    if (determineScale(formatter) == 0) {
        formatter.setParseIntegerOnly(true);
    }
    return formatter;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Aggregations

NumberFormat (java.text.NumberFormat)471 DecimalFormat (java.text.DecimalFormat)92 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 BigDecimal (java.math.BigDecimal)23 Locale (java.util.Locale)22 Map (java.util.Map)18 Test (org.junit.Test)17 ParseException (java.text.ParseException)16 DecimalFormatSymbols (java.text.DecimalFormatSymbols)14 JFreeChart (org.jfree.chart.JFreeChart)13 IOException (java.io.IOException)12 ParsePosition (java.text.ParsePosition)12 XYSeries (org.jfree.data.xy.XYSeries)11 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)11 Intent (android.content.Intent)10 PrintWriter (java.io.PrintWriter)9 View (android.view.View)8 TextView (android.widget.TextView)8 Currency (java.util.Currency)8