Example usage for java.text DecimalFormat applyPattern

List of usage examples for java.text DecimalFormat applyPattern

Introduction

In this page you can find the example usage for java.text DecimalFormat applyPattern.

Prototype

public void applyPattern(String pattern) 

Source Link

Document

Apply the given pattern to this Format object.

Usage

From source file:Main.java

/**
 * Convert time to a string//from  ww w .  jav  a2s.co  m
 *
 * @param millis e.g.time/length from file
 * @return Formatted string (hh:)mm:ss
 */
public static String millisToString(long millis) {
    boolean negative = millis < 0;
    millis = Math.abs(millis);

    millis /= 1000;
    int sec = (int) (millis % 60);
    millis /= 60;
    int min = (int) (millis % 60);
    millis /= 60;
    int hours = (int) millis;

    String time;
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    format.applyPattern("00");
    if (millis > 0) {
        time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
    } else {
        time = (negative ? "-" : "") + min + ":" + format.format(sec);
    }

    return time;
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static Number parseNumber(final String source, final String conversionPattern) throws ParseException {
    DecimalFormat df = DECIMAL_FORMAT.get();
    df.applyPattern(conversionPattern);
    return df.parse(source);
}

From source file:Main.java

private static String millisToString(long millis, boolean text) {
    boolean negative = millis < 0;
    millis = java.lang.Math.abs(millis);

    millis /= 1000;//from w  ww. jav  a 2s  . c  o m
    int sec = (int) (millis % 60);
    millis /= 60;
    int min = (int) (millis % 60);
    millis /= 60;
    int hours = (int) millis;

    String time;
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    format.applyPattern("00");
    if (text) {
        if (millis > 0)
            time = (negative ? "-" : "") + hours + "h" + format.format(min) + "min";
        else if (min > 0)
            time = (negative ? "-" : "") + min + "min";
        else
            time = (negative ? "-" : "") + sec + "s";
    } else {
        if (millis > 0)
            time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
        else
            time = (negative ? "-" : "") + min + ":" + format.format(sec);
    }
    return time;
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static String format(final long number, final String conversionPattern) {
    DecimalFormat df = DECIMAL_FORMAT.get();
    if (conversionPattern != null) {
        df.applyPattern(conversionPattern);
    }/* w  w w.j  av  a 2s .c  o  m*/
    return df.format(number);
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static String format(final double number, final String conversionPattern) {
    DecimalFormat df = DECIMAL_FORMAT.get();
    if (conversionPattern != null) {
        df.applyPattern(conversionPattern);
    }/*from ww w  . j a va 2 s . co  m*/
    return df.format(number);
}

From source file:Main.java

public static String roundWithTwoDecimals(double number) {
    try {/*from w  ww  . j  a  v  a2  s. co  m*/
        // format based on default locale
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        df.setDecimalFormatSymbols(dfs);
        df.applyPattern("#####0.00");
        return df.format(number);
    } catch (IllegalArgumentException e) {
        return null;
    }
}

From source file:com.payu.sdk.helper.SignatureHelper.java

/**
 * The message the signature will use/*from   w  w w .jav  a  2  s .  com*/
 *
 * @param order The order that will have the signature
 * @param merchantId The merchantId into the signature
 * @param key The apiKey of the merchant
 * @param valueFormat The format to use for decimal values
 * @return the message that will go into the signature
 */
private static String buildMessage(Order order, Integer merchantId, String key, String valueFormat) {

    validateOrder(order, merchantId);

    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
    df.applyPattern(valueFormat);

    StringBuilder message = new StringBuilder();
    message.append(key);
    message.append("~");
    message.append(merchantId);
    message.append("~");
    message.append(order.getReferenceCode());
    message.append("~");
    message.append(df.format(order.getAdditionalValue(TX_VALUE).getValue().doubleValue()));
    message.append("~");
    message.append(order.getAdditionalValue(TX_VALUE).getCurrency().toString());

    return message.toString();
}

From source file:Main.java

public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);
    return df.format(amount);
}

From source file:Main.java

public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);
    return df.format(amount);
}

From source file:gob.dp.simco.comun.FunctionUtil.java

public static String formateaDecimal(double numero) {
    if (numero > 0) {
        Locale locale = new Locale("en", "UK");
        String pattern = "###,###.##";

        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
        decimalFormat.applyPattern(pattern);

        String format = decimalFormat.format(numero);
        return format;
    }/*www .  j  a v a  2s .  co m*/
    return null;
}