Java Double Number Format getFormat(double d)

Here you can find the source of getFormat(double d)

Description

_more_

License

Apache License

Parameter

Parameter Description
d _more_

Return

_more_

Declaration

public static DecimalFormat getFormat(double d) 

Method Source Code

//package com.java2s;
/*/*from ww w. j a  v a  2  s . c  om*/
* Copyright (c) 2008-2018 Geode Systems LLC
*
* 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 {
    /** _more_ */
    private static DecimalFormat[] FORMATS = { new DecimalFormat("#0"), new DecimalFormat("#0.0"),
            new DecimalFormat("#0.00"), new DecimalFormat("#0.000"), new DecimalFormat("#0.0000"),
            new DecimalFormat("#0.00000"), };

    /**
     * _more_
     *
     * @param d _more_
     *
     * @return _more_
     */
    public static DecimalFormat getFormat(double d) {
        d = Math.abs(d);
        if ((d > 10000) || (d == 0)) {
            return FORMATS[0];
        }
        if (d > 1000) {
            return FORMATS[1];
        }
        if (d > 100) {
            return FORMATS[2];
        }
        if (d > 10) {
            return FORMATS[3];
        }
        if (d > 1) {
            return FORMATS[4];
        }

        return FORMATS[4];
    }
}

Related

  1. formatDoublePrecise(double source, int decimals, int precision, StringBuffer target)
  2. formatDoubleToFixedLength(String value, int length)
  3. formatDoubleToString(double d)
  4. formatDoubleToString(double n)
  5. formatDoubleWithPadding(String value, int length, char pad)
  6. getFormat(double value)
  7. getFormatAmount(Double d)
  8. getFormattedNumber(Double number, int decimals, Locale locale)
  9. getFormattedString(double d, int numDecimalPlaces)