Java Array to String arrayToStringWithDifferenceOrientedFormat(double[] values, int minDigits)

Here you can find the source of arrayToStringWithDifferenceOrientedFormat(double[] values, int minDigits)

Description

array To String With Difference Oriented Format

License

Apache License

Declaration

public static String[] arrayToStringWithDifferenceOrientedFormat(double[] values, int minDigits) 

Method Source Code


//package com.java2s;
/*//from   w  ww  . j a v  a2  s  .  c  o m
 * Copyright 2015 Lutz Fischer <lfischer at staffmail.ed.ac.uk>.
 *
 * 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.util.ArrayList;

public class Main {
    public static String[] arrayToStringWithDifferenceOrientedFormat(double[] values, int minDigits) {

        String formatString = formatStringForPrettyPrintingRelatedValues(values, minDigits);

        String[] ret = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            ret[i] = String.format(formatString, values[i]);
        }
        return ret;

    }

    public static String formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits) {

        double diff = minDiff(values, true);

        // turn that into the decimal by using log10 and round that down
        double roundingFactor = Math.round(Math.log(diff) / Math.log(10) - 0.5);

        // we don't want to round away any digits before the dot
        if (roundingFactor > -minDigits)
            roundingFactor = -minDigits;

        double normFactor = Math.pow(10, roundingFactor);
        //if (roundingFactor<0)
        roundingFactor = -roundingFactor;

        return "%." + String.format("%.0f", roundingFactor) + "f";
    }

    public static String formatStringForPrettyPrintingRelatedValues(double[] values, int minDigits, int maxDigits) {

        double[] roundedvalues = new double[values.length];
        double factor = Math.pow(10, maxDigits);

        // make sure that no difference smaller then the number of maximum 
        // digits is still there, by rounding all values to these digits.
        for (int i = 0; i < values.length; i++) {
            roundedvalues[i] = Math.round(values[i] * factor) / factor;
        }

        // get the smallest difference greater than 0
        double diff = minDiff(roundedvalues, true);

        // turn that into the decimal by using log10 and round that down
        double roundingFactor = Math.round(Math.log(diff) / Math.log(10) - 0.5);

        // we don't want to round away any digits before the dot
        if (roundingFactor > -minDigits)
            roundingFactor = -minDigits;

        double normFactor = Math.pow(10, roundingFactor);
        //if (roundingFactor<0)
        roundingFactor = -roundingFactor;

        return "%." + String.format("%.0f", roundingFactor) + "f";
    }

    public static double minDiff(ArrayList<Double> list, boolean gz) {
        int l = list.size();
        double minDiff = Double.POSITIVE_INFINITY;
        if (gz) {
            for (int f = 0; f < l; f++) {
                for (int s = f + 1; s < l; s++) {
                    double diff = Math.abs(list.get(f) - list.get(s));
                    if (diff > 0)
                        minDiff = Math.min(minDiff, diff);
                }
            }
            if (minDiff == Double.POSITIVE_INFINITY)
                return 0;

            return minDiff;
        }
        // we don't care about zeros
        for (int f = 0; f < l; f++) {
            for (int s = f + 1; s < l; s++) {
                double diff = Math.abs(list.get(f) - list.get(s));
                minDiff = Math.min(minDiff, diff);
            }
        }

        return minDiff;
    }

    /**
     * minimum difference between any of the values in the array
     * if 
     * @param list
     * @param greaterZero
     * @return 
     */
    public static double minDiff(double[] list, boolean greaterZero) {
        int l = list.length;
        double minDiff = Double.POSITIVE_INFINITY;
        if (greaterZero) {
            for (int f = 0; f < l; f++) {
                for (int s = f + 1; s < l; s++) {
                    double diff = Math.abs(list[f] - list[s]);
                    if (diff > 0)
                        minDiff = Math.min(minDiff, diff);
                }
            }
            if (minDiff == Double.POSITIVE_INFINITY)
                return 0;

            return minDiff;
        }
        // we don't care about zeros
        for (int f = 0; f < l; f++) {
            for (int s = f + 1; s < l; s++) {
                double diff = Math.abs(list[f] - list[s]);
                minDiff = Math.min(minDiff, diff);
            }
        }

        return minDiff;
    }
}

Related

  1. arrayToString2D(String title, String innerTitle, double[][] vect)
  2. arrayToStringArray(boolean[] values)
  3. arrayToStringDelimited(final String[] array, final String delimiter)
  4. arrayToStringNullable(String[] a)
  5. arrayToStrings(String[] s, String delimiter)
  6. arrayToStringWithPrefix(T[] array, String splitter, String prefix)
  7. asString(final String[] array)
  8. asString(int[] objects, String separator)
  9. asString(String[] arrayParameter)