The Coder
The Coder

Reputation: 2632

Converting Decimal (Double) Value < 0.001 to String in Java

I have written a small method which is utilised for most of the Object to String conversions.

public String str(Object object) {
    return object == null ? "" : object.toString();
}

When passing double d = 0.0003 to this method, I get an output of 3.0E-4. So I have altered the method like this.

public String str(Object object) {
    return object == null ? "" : object instanceof Double && (double) object < 0.001 ? String.format("%.10f", object)
            : object.toString();
}

But I kinda feel bad to check instanceof for every conversions which is an additional check for non double objects. Is this the only way that I can convert double to exact string value or is it possible to convert correctly without an additional instance of check. Since if the object satisfies instanceof Double, casting is of free cost and so I'm not worried about performance on casting.

I tried these steps, all produces E output except String.format

double d = 0.0003;               // 3.0E-4
System.out.println(String.valueOf(d));          // 3.0E-4
System.out.println(Double.toString(d));         // 3.0E-4
System.out.println(String.format("%.10f", d));   // 0.0003000000

Upvotes: 0

Views: 1938

Answers (3)

Olexander Yushko
Olexander Yushko

Reputation: 2904

You can use String.valueOf():

public static void main(String[] args) {
    final List<String> doubleValues = new ArrayList<>();
    double value = 0.0;
    doubleValues.add("5.01");
    doubleValues.add("0.002");
    doubleValues.add("0.00024");
    doubleValues.add("0.000022");

    for (String elem : doubleValues) {
        value += Double.valueOf(elem);
    }
    System.out.printf("%.10f", value); // Formatting in print
    System.out.println();

    System.out.println(String.format("%.10f", value)); // Print with String formatting

    double v = Double.parseDouble(String.valueOf(value)); //convert decimal number to double for print or calc
    System.out.println(v);
}

Upvotes: 1

user109260
user109260

Reputation: 878

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PrintNumbers {
    public static void main(String[] args) {
        printNumberWOScientificNotations(0.1);
        printNumberWOScientificNotations(0.001);
        printNumberWOScientificNotations(0.00001);
        System.out.println(0.00001);
    }

    static void printNumberWOScientificNotations(Object number) {
        // Check if in scientific notation
        if (String.valueOf(number).toLowerCase().contains("e")) {
            System.out.println("Converting from e to number with 25 maximum fraction digits.");
            NumberFormat formatter = new DecimalFormat();
            formatter.setMaximumFractionDigits(25);
            System.out.println(formatter.format(new Double(number.toString())));
        } else {
            System.out.println(String.valueOf(number));
        }
    }
}

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24156

you can provide different functions, for different types, like:

public static String str(final Object object) {
    return object == null ? "" : object.toString();
}

public static String str(final Double d) {
    return d == null ? "" : str(d.doubleValue());
}

public static String str(final double d) {
    return d < 0.001 ? String.format("%.10f", d) : String.valueOf(d);
}

Upvotes: 2

Related Questions