Java Locale Format formatDecimalDisplay(final double _numberToFormat, final String _formatToApply)

Here you can find the source of formatDecimalDisplay(final double _numberToFormat, final String _formatToApply)

Description

Utility function for applying formatting to numbers.

License

Open Source License

Parameter

Parameter Description
_numberToFormat The number to format with the _formatToApply.
_formatToApply The format to apply to the supplied number

Return

The formatted number as a string.

Declaration



public static String formatDecimalDisplay(final double _numberToFormat, final String _formatToApply) 

Method Source Code


//package com.java2s;
/*//from  ww w.  ja  va2 s. c  om
 * Created 2012
 * 
 * This file is part of Topological Data Analysis
 * edu.duke.math.tda
 * TDA is licensed from Duke University.
 * Copyright (c) 2012-2014 by John Harer
 * All rights reserved.
 * 
 */

import java.util.*;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Main {
    /**
     * Utility function for applying formatting to numbers.
     * @param _numberToFormat The number to format with the _formatToApply.
     * @param _formatToApply The format to apply to the supplied number
     * 
     * @return The formatted number as a string.
     */
    // 10/13/2005 hjs
    // Better (more robust) method for applying formatting to numbers
    public static String formatDecimalDisplay(final double _numberToFormat, final String _formatToApply) {

        String formattedNumber;

        // hjs 7/18/2007 When no number is passed in, or the format is invalid,
        // simply return the passed-in value (note: value can be NaN, which we can't format)
        // hjs 9/27/2007 Fix a localization issue (thanks to F. Menolascina and M. Clerx)
        try {

            DecimalFormat decimalFormatForDisplay = new DecimalFormat(_formatToApply,
                    new DecimalFormatSymbols(Locale.US));

            formattedNumber = decimalFormatForDisplay.format(_numberToFormat);
        } catch (Exception e) {

            // Simply use whatever number was supplied (including NaN, etc)
            formattedNumber = new Double(_numberToFormat).toString();
        }

        return formattedNumber;
    }
}

Related

  1. formatDateByFormat(Date date, String format)
  2. formatDateTime(final Date date)
  3. formatDateTime(java.util.Date date, String format, String locale, String timeZone)
  4. formatDateToSQLString(Date srcDate)
  5. formatDecimal(BigInteger b)
  6. formatDouble(double in)
  7. formatDouble(double inVal, int inDecs)
  8. formatDouble(Double localDouble, int scale)
  9. formatDouble(double number)