Java Number Format Pattern formatNumber(Number number)

Here you can find the source of formatNumber(Number number)

Description

Examples: 1 -> 1 12d -> 12 123d -> 123 1234d -> 1.234E03 12345d -> 12.345E03 123.1234567890d -> 123.123457 1234.1234567890d -> 1.23412346E03 1.12345678901234567890d -> 1.12345679 12345.1234567890d -> 12.3451235E03 0.000000000001234567890d -> 1.23456789E-12

License

Apache License

Parameter

Parameter Description
number a parameter

Declaration

public static String formatNumber(Number number) 

Method Source Code


//package com.java2s;
/*//ww  w . j  a  v a2s .c  om
 *   Copyright 2012
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    private static ThreadLocal<DecimalFormat> nf = new ThreadLocal() {
        @Override
        protected Object initialValue() {
            return new DecimalFormat("##0.######E00");
        }
    };

    /**
     *
     * Examples:
     *
     * 1                           -> 1
     * 12d                         -> 12
     * 123d                        -> 123
     * 1234d                       -> 1.234E03
     * 12345d                      -> 12.345E03
     * 123.1234567890d             -> 123.123457
     * 1234.1234567890d            -> 1.23412346E03
     * 1.12345678901234567890d     -> 1.12345679
     * 12345.1234567890d           -> 12.3451235E03
     * 0.000000000001234567890d    -> 1.23456789E-12
     *
     * @param number
     * @return
     */
    public static String formatNumber(Number number) {
        return nf.get().format(number).replace("E00", "");
    }
}

Related

  1. formatNumber(int num)
  2. formatNumber(int source)
  3. formatNumber(long number)
  4. formatNumber(long number)
  5. formatNumber(Number num)
  6. FormatNumber(Object o,String patter)
  7. formatoDecimalPunto(String numero)
  8. formatQuantity(Long quantity)
  9. formattedDuration(long pStartTime)