Java Number Format formatNumber(float num, int fragmentSize)

Here you can find the source of formatNumber(float num, int fragmentSize)

Description

fragmentSize must greater 1.

License

Open Source License

Parameter

Parameter Description
num a parameter
fragmentSize a parameter

Declaration

public static String formatNumber(float num, int fragmentSize) 

Method Source Code

//package com.java2s;
/* //ww  w.  j av a2  s. co  m
 * =============================================================
 * Copyright (C) 2007-2011 Edgenius (http://www.edgenius.com)
 * =============================================================
 * License Information: http://www.edgenius.com/licensing/edgenius/2.0/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0
 * as published by the Free Software Foundation.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * http://www.gnu.org/licenses/gpl.txt
 *  
 * ****************************************************************
 */

public class Main {
    /**
     * fragmentSize must greater 1. Otherwise please use int a = (int)f;
     * 
     * @param num
     * @param fragmentSize
     * @return
     */
    public static String formatNumber(float num, int fragmentSize) {
        String ret = Float.toString(num);
        int len;
        if ((len = ret.indexOf(".")) != -1 && len + fragmentSize + 1 < ret.length()) {
            ret = ret.substring(0, len + fragmentSize + 1);
        }
        return ret;
    }
}

Related

  1. formatNumber(double value)
  2. formatNumber(double value)
  3. formatNumber(final long number)
  4. formatNumber(final String number)
  5. formatNumber(final StringBuilder buf, final long number)
  6. formatNumber(int number)
  7. formatNumber(int number, int zeros)
  8. formatNumber(int theNumber, int theNumberOfDigits)
  9. formatNumber(int value, int width, char c)