Java Fraction Format format(final double d)

Here you can find the source of format(final double d)

Description

Formats a number.

License

Open Source License

Parameter

Parameter Description
d a number.

Return

a string containing a pretty print of the number.

Declaration

public static String format(final double d) 

Method Source Code

//package com.java2s;
/*       //from  w w  w.j  av  a 2  s  .  c om
 * DSI utilities
 *
 * Copyright (C) 2002-2009 Sebastiano Vigna 
 *
 *  This library is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as published by the Free
 *  Software Foundation; either version 2.1 of the License, or (at your option)
 *  any later version.
 *
 *  This library is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

public class Main {
    /** A reasonable format for real numbers. */
    private static final java.text.NumberFormat FORMAT_DOUBLE = new java.text.DecimalFormat("#,##0.00");
    /** A reasonable format for integers. */
    private static final java.text.NumberFormat FORMAT_LONG = new java.text.DecimalFormat("#,###");

    /** Formats a number.
     *
     * <P>This method formats a double separating thousands and printing just two fractional digits.
     * @param d a number.
     * @return a string containing a pretty print of the number.
     */
    public static String format(final double d) {
        final StringBuffer s = new StringBuffer();
        return FORMAT_DOUBLE.format(d, s, new java.text.FieldPosition(0)).toString();
    }

    /** Formats a number.
     *
     * <P>This method formats a long separating thousands.
     * @param l a number.
     * @return a string containing a pretty print of the number.
     */
    public static String format(final long l) {
        final StringBuffer s = new StringBuffer();
        return FORMAT_LONG.format(l, s, new java.text.FieldPosition(0)).toString();
    }
}

Related

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