Java Fraction Format format(double value, int decimalPlace)

Here you can find the source of format(double value, int decimalPlace)

Description

Format the given value to the number of decimal places specified.

License

Open Source License

Parameter

Parameter Description
value The value that needs to be formatted.
decimalPlace Number of decimal places to be formatted.

Return

The formatted string.

Declaration

public static String format(double value, int decimalPlace) 

Method Source Code

//package com.java2s;
/**// ww w  .  j ava2s  . c  om
*  Copyright 2012 NCS Pte. Ltd. All Rights Reserved
*
*  This software is confidential and proprietary to NCS Pte. Ltd. You shall
*  use this software only in accordance with the terms of the license
*  agreement you entered into with NCS.  No aspect or part or all of this
*  software may be reproduced, modified or disclosed without full and
*  direct written authorisation from NCS.
*
*  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
*  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
*  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
*  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
*  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**
     * Format the given value to the number of decimal places specified.
     * The output string will group the numbers using the thousand-separator.
     * Eg. the number "12345" -> "12,345" as the result.
     *
     * @param value The value that needs to be formatted.
     * @param decimalPlace Number of decimal places to be formatted.
     * @return The formatted string.
     */
    public static String format(double value, int decimalPlace) {
        NumberFormat nft = NumberFormat.getInstance();

        nft.setMinimumFractionDigits(decimalPlace);
        nft.setMaximumFractionDigits(decimalPlace);

        return nft.format(value);
    }

    /**
     * Format the given value to the number pattern specified.
     *
     * The pattern should use the following symbols.
     *
     * that the result produces "$1,234.40" with "1234.4".
     *
     * <blockquote>
     * <table border >
     *   <th>Symbol</th>
     *   <th>Description</th>
     * </tr>
     *
     * <tr> <td>0</td> <td>a digit</td> </tr>
     * <tr> <td>#</td> <td>a digit, zero shows as absent</td> </tr>
     * <tr> <td>.</td> <td>placeholder for decimal separator</td> </tr>
     * <tr> <td>,</td> <td>placeholder for grouping separator</td> </tr>
     * <tr> <td>E</td> <td>separates mantissa and exponent for exponential formats</td> </tr>
     * <tr> <td>;</td> <td>separates formats</td> </tr>
     * <tr> <td>-</td> <td>default negative prefix</td> </tr>
     * <tr> <td>%</td> <td>multiply by 100 and show as percentage</td> </tr>
     * <tr> <td>?</td> <td>multiply by 1000 and show as per mille</td> </tr>
     * <tr> <td>&curren;</td> <td>currency sign; replaced by currency symbol; if doubled, replaced by international currency symbol;  if present in a pattern, the monetary decimal separator is used instead of the decimal separator </td> </tr>
     * <tr> <td>X</td> <td>any other characters can be used in the prefix or suffix</td> </tr>
     * <tr> <td>'</td> <td>used to quote special characters in a prefix or suffix</td> </tr>
     *
     * </table >
     * </blockquote>
     *
     * @param value The value that needs to be formatted.
     * @param pattern The pattern string to be applied to double value.
     * @return The formatted string.
     */
    public static String format(double value, String pattern) {
        NumberFormat nft = NumberFormat.getInstance();

        ((DecimalFormat) nft).applyPattern(pattern);

        return nft.format(value);
    }
}

Related

  1. format(double value)
  2. format(double value)
  3. format(double value)
  4. format(double value)
  5. format(double value, double unit)
  6. format(double value, String formatString)
  7. format(double x, int max, int min)
  8. format(final double d)
  9. format(final double number)