Java Double Number to String doubleToString(final double number)

Here you can find the source of doubleToString(final double number)

Description

double To String

License

MIT License

Declaration

public static String doubleToString(final double number) 

Method Source Code


//package com.java2s;
/*//w  w  w  .  j av  a 2  s .com
 * Copyright 2009-2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

import java.text.DecimalFormat;

public class Main {
    public static String doubleToString(final double number) {

        final double abs = Math.abs(number);

        final DecimalFormat format;

        if (abs < 100.0) {
            format = new DecimalFormat("#.#####");
        } else if (abs < 1000.0) {
            format = new DecimalFormat("#.####");
        } else if (abs < 10000.0) {
            format = new DecimalFormat("#.###");
        } else if (abs < 30000.0) {
            format = new DecimalFormat("#.##");
        } else if (abs < 100000.0) {
            format = new DecimalFormat("#.#");
        } else {
            format = new DecimalFormat("#");
        }

        return format.format(number);
    }
}

Related

  1. doubleToString(double value, int afterDecimalPoint)
  2. doubleToString(double value, int fractionDigits)
  3. doubleToString(double value, int maximum, int minimum)
  4. doubleToString(double value, String format)
  5. doubleToString(final double d, final int decimalPlaces)
  6. doubleToSz(double dval)
  7. doubleToVisualString(double d)