Example usage for java.text NumberFormat setMaximumIntegerDigits

List of usage examples for java.text NumberFormat setMaximumIntegerDigits

Introduction

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

Prototype

public void setMaximumIntegerDigits(int newValue) 

Source Link

Document

Sets the maximum number of digits allowed in the integer portion of a number.

Usage

From source file:org.liferayhub.pc.service.impl.PowerConsoleServiceImpl.java

private String getUptime(long time) {
    long uptimeDiff = System.currentTimeMillis() - time;
    long days = uptimeDiff / Time.DAY;
    long hours = (uptimeDiff / Time.HOUR) % 24;
    long minutes = (uptimeDiff / Time.MINUTE) % 60;
    long seconds = (uptimeDiff / Time.SECOND) % 60;

    NumberFormat numberFormat = NumberFormat.getInstance();

    numberFormat.setMaximumIntegerDigits(2);
    numberFormat.setMinimumIntegerDigits(2);
    String response = "";
    if (days > 0) {
        response = days + (days > 1 ? " days " : " day ");
    }/*from  ww w .j a v a 2 s  .c  om*/
    response += numberFormat.format(hours) + ":" + numberFormat.format(minutes) + ":"
            + numberFormat.format(seconds);
    return response;
}

From source file:org.openestate.io.core.NumberUtils.java

/**
 * Write a number into a string value.//from   w w w  .j av a  2s  . co  m
 *
 * @param value
 * the number to write
 *
 * @param integerDigits
 * maximal number of integer digits
 *
 * @param fractionDigits
 * maximal number of fraction digits
 *
 * @param locale
 * locale for decimal separator (using {@link Locale#ENGLISH} if null)
 *
 * @return
 * the formatted number
 */
public static String printNumber(Number value, int integerDigits, int fractionDigits, Locale locale) {
    if (value == null)
        return null;
    NumberFormat format = NumberFormat.getNumberInstance((locale != null) ? locale : Locale.ENGLISH);
    format.setMaximumIntegerDigits(integerDigits);
    format.setMaximumFractionDigits(fractionDigits);
    format.setMinimumFractionDigits(0);
    format.setGroupingUsed(false);
    return format.format(value);
}

From source file:org.polymap.kaps.importer.test.NumberFormatterTest.java

public void testThousands() {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
    nf.setMaximumIntegerDigits(10);
    nf.setMaximumFractionDigits(10);/*from   w  w  w .java  2  s. c om*/
    nf.setMinimumIntegerDigits(10);
    nf.setMinimumFractionDigits(10);

    System.out.println(nf.format(12345));
}

From source file:org.rhq.core.util.StringUtil.java

public static String formatDuration(long duration, int scale, boolean minDigits) {
    long hours;/*from  w w  w .ja  v  a 2  s .  c o m*/
    long mins;
    int digits;
    double millis;

    hours = duration / 3600000;
    duration -= hours * 3600000;

    mins = duration / 60000;
    duration -= mins * 60000;

    millis = (double) duration / 1000;

    StringBuilder buf = new StringBuilder();

    if ((hours > 0) || (minDigits == false)) {
        buf.append(((hours < 10) && (minDigits == false)) ? ("0" + hours) : String.valueOf(hours)).append(':');
        minDigits = false;
    }

    if ((mins > 0) || (minDigits == false)) {
        buf.append(((mins < 10) && (minDigits == false)) ? ("0" + mins) : String.valueOf(mins)).append(':');
        minDigits = false;
    }

    // Format seconds and milliseconds
    NumberFormat fmt = NumberFormat.getInstance();
    digits = (((minDigits == false) || ((scale == 0) && (millis >= 9.5))) ? 2 : 1);
    fmt.setMinimumIntegerDigits(digits);
    fmt.setMaximumIntegerDigits(2); // Max of 2
    fmt.setMinimumFractionDigits(0); // Don't need any
    fmt.setMaximumFractionDigits(scale);

    buf.append(fmt.format(millis));

    return buf.toString();
}