Java Decimal Format toString(final Double value)

Here you can find the source of toString(final Double value)

Description

to String

License

Apache License

Declaration

private static String toString(final Double value) 

Method Source Code


//package com.java2s;
/*//from w  w  w .  ja  v a 2  s .c om
 * Copyright (c) 2012-2014 Spotify AB
 *
 * 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.NumberFormat;
import java.util.Locale;

public class Main {
    private static ThreadLocal<NumberFormat> formatters = new ThreadLocal<NumberFormat>() {

        @Override
        protected NumberFormat initialValue() {
            NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
            nf.setMaximumFractionDigits(9); // 9 is randomly picked
            nf.setGroupingUsed(false);
            return nf;
        }
    };

    private static String toString(final Double value) {
        if (Double.isNaN(value) || Double.isInfinite(value)) {
            // http://munin-monitoring.org/wiki/network-protocol
            // "Numeric value, or 'U'"
            return "U";
        } else {
            return formatters.get().format(value);
        }
    }

    private static String toString(final Float value) {
        if (Float.isNaN(value) || Float.isInfinite(value)) {
            // http://munin-monitoring.org/wiki/network-protocol
            // "Numeric value, or 'U'"
            return "U";
        } else {
            return formatters.get().format(value);
        }
    }

    /**
     * Used for gauges where the type of the value is not known
     * @param value The value, can be of any type or null, but {@link Number} will be properly
     *              formatted
     * @return The formatted value, toString() used for non-Number values, null for null input
     *         values
     */
    public static String toString(final Object value) {
        if (value instanceof Double) {
            return toString((Double) value);
        } else if (value instanceof Float) {
            return toString((Float) value);
        } else if (value instanceof Number) {
            return formatters.get().format(value);
        } else if (value != null) {
            // fallback if something not a number is used
            return value.toString();
        } else {
            return null;
        }
    }
}

Related

  1. toString(double value)
  2. toString(double value, int precision)
  3. toString(double[] array)
  4. toString(Double[] input)
  5. toString(double[][] a, int decimals)
  6. toString(final double value, final int maxDecimalPlaces)
  7. toStringForHumans(double doubleToConvert)
  8. toStringNoDigits(double[] v)
  9. toStringScientific(double x)