Domanda

I am trying to convert a list of doubles from a string format into a double format. I did this by putting them into a list and using the Double.parseDouble(string) method.

This works for most numbers but it gives an unwanted output when the double contains a trailing zero the parseDouble method is removing it. I don't want this to be removed.

String[] values = {"124.50", "45.801", "-15.210"};
List<Double> nums = new ArrayList<Double>();

for(String s: values)
nums.add(Double.parseDouble(s));
Collections.sort(nums);

for(Double d: nums){
    System.out.print(d + " ");
}

This yields the output:

-15.21 45.801 124.5

But I want the trailing zeros. The problem with using a formatted line is that I would have to specify the floating point accuracy that I want when printing values, but I don't have any specific desire to make the numbers accurate to a certain point, merely leave the zeros alone if they are there.

Am I approaching this wrong?

È stato utile?

Soluzione

You could store a reference to the original string and print that instead of the parsed double

public static void main(String[] args) {
    String[] values = {"124.50", "45.801", "-15.210"};
    List<MyDouble> nums = new ArrayList<MyDouble>();

    for(String s: values)
    nums.add(new MyDouble(s));
    Collections.sort(nums);

    for(MyDouble d: nums){
        System.out.print(d + " ");
    }
}

static class MyDouble implements Comparable<MyDouble> {
    final double val;
    final String string;
    MyDouble(String str) {
        string = str;
        val = Double.parseDouble(str);
    }

    @Override
    public String toString() {
        return string;
    }

    @Override
    public int compareTo(MyDouble o) {
        return (int) (val - o.val);
    }
}

Result:

-15.210 45.801 124.50

Altri suggerimenti

You could use BigDecimal to handle the values. BigDecimal preserves trailing zeroes.

String[] values = {"124.50", "45.801", "-15.210"};
List<BigDecimal> nums = new ArrayList<BigDecimal>();

for(String s: values)
    nums.add(new BigDecimal(s));
Collections.sort(nums);

for(BigDecimal d: nums) {
    System.out.print(d + " ");
}

If you always want n decimal digits, you can do the following:

final DecimalFormat df = new DecimalFormat("#.000");
for (Double d : nums) {
    System.out.print(df.format(d) + " ");
}

More specifically, this solution works for you but as @BrianRoach mentioned in his comment, there is no such thing as a trailing zero when talking about actual double numbers.

final String[] values = {"124.50", "45.801", "-15.210"};
final List<Double> nums = new ArrayList<>();
final List<Double> bkpNums = new ArrayList<>();

for (final String s : values) {
    double d = Double.parseDouble(s);
    nums.add(d);
    bkpNums.add(d);
}
Collections.sort(nums);

for (Double d : nums) {
    System.out.print(values[bkpNums.indexOf(d)] + " ");
}

Try to use:

DecimalFormat df = new DecimalFormat("###.#");

while printing numbers

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top