Java Locale Format formatFloat(float f, int numDecPlaces, boolean forceFractions)

Here you can find the source of formatFloat(float f, int numDecPlaces, boolean forceFractions)

Description

Formats the given float to a String with the specified number of decimal places.

License

Open Source License

Parameter

Parameter Description
f the number to format
numDecPlaces the maximum number of decimal places
forceFractions always format with maximum number of fractions?

Return

the formatted String.

Declaration

public static final String formatFloat(float f, int numDecPlaces, boolean forceFractions) 

Method Source Code

//package com.java2s;
/**/*from   w w  w. j a  va2 s  .c  om*/
 * Copyright (C) 2009-2014 Cars and Tracks Development Project (CTDP).
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU 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.
 */

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    private static final NumberFormat formatter = DecimalFormat.getNumberInstance(Locale.US);

    /**
     * Formats the given float to a String with the specified number of decimal places.
     * 
     * @param f the number to format
     * @param numDecPlaces the maximum number of decimal places
     * @param forceFractions always format with maximum number of fractions?
     * @param forceSign always show the sign?
     * 
     * @return the formatted String.
     */
    public static final String formatFloat(float f, int numDecPlaces, boolean forceFractions, boolean forceSign) {
        if (numDecPlaces == 0) {
            if (forceSign) {
                if (f == 0f)
                    return ("+0");

                if (f > 0f)
                    return ("+" + String.valueOf(Math.round(f)));

                return (String.valueOf(Math.round(f)));
            }

            return (String.valueOf(Math.round(f)));
        }

        /*
        int p = (int)Math.pow( 10, numDecPlaces );
            
        int i = (int)f;
        String s = String.valueOf( i ) + ".";
            
        f -= i;
        f *= p;
        i = Math.round( f );
            
        s += String.valueOf( i );
            
        return ( s );
        */

        formatter.setMaximumFractionDigits(numDecPlaces);
        formatter.setMinimumFractionDigits(forceFractions ? numDecPlaces : 0);

        if (forceSign && (f >= 0f)) {
            return ("+" + formatter.format(f));
        }

        return (formatter.format(f));
    }

    /**
     * Formats the given float to a String with the specified number of decimal places.
     * 
     * @param f the number to format
     * @param numDecPlaces the maximum number of decimal places
     * @param forceFractions always format with maximum number of fractions?
     * 
     * @return the formatted String.
     */
    public static final String formatFloat(float f, int numDecPlaces, boolean forceFractions) {
        return (formatFloat(f, numDecPlaces, forceFractions, false));
    }
}

Related

  1. formatDoubleToCSV(double d)
  2. formatDuration(long nanoTime)
  3. formatedDate(Date date)
  4. formatElapsedTime(double runTime)
  5. formatEn(T date, String pattern)
  6. formatFloat(Float f, int style)
  7. formatFloat(float val)
  8. formatFraction2(final Number value)
  9. formatInCurrentDefaultLocale(double d)