Search in sources :

Example 1 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols 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 2 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project zeppelin by apache.

the class TransferListener method transferSucceeded.

@Override
public void transferSucceeded(TransferEvent event) {
    transferCompleted(event);
    TransferResource resource = event.getResource();
    long contentLength = event.getTransferredBytes();
    if (contentLength >= 0) {
        String type = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
        String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B";
        String throughput = "";
        long duration = System.currentTimeMillis() - resource.getTransferStartTime();
        if (duration > 0) {
            DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH));
            double kbPerSec = (contentLength / 1024.0) / (duration / 1000.0);
            throughput = " at " + format.format(kbPerSec) + " KB/sec";
        }
        logger.info(type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + throughput + ")");
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) TransferResource(org.sonatype.aether.transfer.TransferResource) DecimalFormat(java.text.DecimalFormat)

Example 3 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project cucumber-jvm by cucumber.

the class Stats method printDuration.

private void printDuration(PrintStream out) {
    out.print(String.format("%dm", (totalDuration / ONE_MINUTE)));
    DecimalFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(locale));
    out.println(format.format(((double) (totalDuration % ONE_MINUTE)) / ONE_SECOND) + "s");
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 4 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project jphp by jphp-compiler.

the class NumUtils method format.

@FastMethod
@Signature({ @Arg("number"), @Arg("pattern"), @Arg(value = "decSep", optional = @Optional(".")), @Arg(value = "groupSep", optional = @Optional(",")) })
public static Memory format(Environment env, Memory... args) {
    try {
        char decSep = args[2].toChar();
        char groupSep = args[3].toChar();
        DecimalFormat decimalFormat;
        if (decSep == '.' && groupSep == ',')
            decimalFormat = new DecimalFormat(args[1].toString(), DEFAULT_DECIMAL_FORMAT_SYMBOLS);
        else {
            DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
            formatSymbols.setZeroDigit('0');
            formatSymbols.setDecimalSeparator(decSep);
            formatSymbols.setGroupingSeparator(groupSep);
            decimalFormat = new DecimalFormat(args[1].toString(), formatSymbols);
        }
        return new StringMemory(decimalFormat.format(args[0].toDouble()));
    } catch (IllegalArgumentException e) {
        return Memory.FALSE;
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) StringMemory(php.runtime.memory.StringMemory) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 5 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols 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

DecimalFormatSymbols (java.text.DecimalFormatSymbols)127 DecimalFormat (java.text.DecimalFormat)88 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 BigDecimal (java.math.BigDecimal)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3