Formats a number in a specified pattern. - Java java.lang

Java examples for java.lang:double Format

Description

Formats a number in a specified pattern.

Demo Code

/*//from  w ww . j a v  a 2 s .c om
 * Copyright (c) 2014 mgamelabs
 * To see our full license terms, please visit https://github.com/mgamelabs/mengine/blob/master/LICENSE.md
 * All rights reserved.
 */
//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    /**
     * Formats a number in a specified pattern.
     *
     * @param input  The number to format.
     * @param format The formatting pattern, e.g. "0.00" (Number with 2 decimal digits).
     * @return The formatted number.
     */
    public static String format(double input, String format) {
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(input);
    }
}

Related Tutorials