Java Double Number to String doublesToStrings(final double... numbers)

Here you can find the source of doublesToStrings(final double... numbers)

Description

doubles To Strings

License

MIT License

Declaration

public static String[] doublesToStrings(final double... numbers) 

Method Source Code


//package com.java2s;
/*/* w ww .  j  a  v a  2 s.  co m*/
 * 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[] doublesToStrings(final double... numbers) {
        if (numbers == null) {
            return null;
        }

        final String[] strings = new String[numbers.length];

        int index = 0;

        for (final double number : numbers) {
            strings[index++] = doubleToString(number);
        }

        return strings;
    }

    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. double2String(double d)
  2. double2String(double value)
  3. doubleToAtLeastString(double d, int length)
  4. doubleToCoreString(double number)
  5. doubleToDollarString(double val)
  6. doubleToFixedString(double d, int length)