How to Print a Double Value Without Scientific Notation Using Java

How do I print a double value without scientific notation using Java?

You could use printf() with %f:

double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);

This will print dexp: 12345678.000000. If you don't want the fractional part, use

System.out.printf("dexp: %.0f\n", dexp);

0 in %.0f means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f. By default fractional part is printed up to 6 decimal places.

This uses the format specifier language explained in the documentation.

The default toString() format used in your original code is spelled out here.

Formatting A Double In A String Without Scientific Notation

Use just %f instead of %.0f.

import java.math.BigDecimal;

public class Main {
public static void main(String[] args) {
double foo = 123456789.1234;
String str = String.format("%f", foo);
System.out.println(str);

// If you want to get rid of the trailing zeros
str = new BigDecimal(str).stripTrailingZeros().toString();
System.out.println(str);
}
}

Output:

123456789.123400
123456789.1234

Convert string to double without scientific notation

The methods you have used do not return 1.08595E8, instead, they return the number and what you are complaining about is the representation of that number in the console (or as a String).

However, you can specify how to output a doubleyourself with a specified formatting, see this example:

public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
double:                             1.085950005E8
engineering: 108595000.5
plain: 108595000.5
rounded with fix decimal places: 108595000.50

How to Avoid Scientific Notation in Double?

Check answer here. You can write

moneyToGet.setText(String.format("%.0f", priceValue));

Formatting Double to print without scientific notation using DecimalFormat

There is indeed a difference between Java's and Android's DecimalFormat class, and they output different results, despite taking the exact same arguments.

This was enough for me to try Henry's approach, and now that I have I see that I have gained an extra 2 places of precision. I am also confident that the values are calculated accurately, as only sums and multiplications are involved.

This is the modified code I ended up using:

...
long javaTime = new Date().getTime();
long nanoTime = System.nanoTime();
long newtimestamp = javaTime * 1000000 + // Compute the timestamp
(event.timestamp - nanoTime); // in nanos first
String longStr = Long.valueOf(newtimestamp).toString();
String tsString = longStr.substring(0, longStr.length()-6) +// Format the output string
"." + longStr.substring(longStr.length()-6); // to have the comma in the
// correct space.
...

Can a Double be formatted without scientific notation?

Is there a way from a String say "1234567890" to retrieve a Double whose value is 1234567890 and not 1.23456789E9 as it is being done by default by Double.parseDouble(FormattedString)?

Your question doesn't really make sense. 1234567890 is the same value as 1.23456789E9 and a double represents one of them, if and only if it also represents the other.

I am aware of the existance of DecimalFormat, however take into account using this formater will give me a String formated correctly say : "#######.E0" but what I really need is a Double with such format, however when doing Double.parseDouble(FormatedString) I will loose such format.

No, there is no way to construct a Double so that it is displayed in a certain way. The toString method for Double is what it is, and it can't be changed.

The only thing you can do is to for instance use DecimalFormat or String.format but as you've noted, you'll always end up with a String.



Related Topics



Leave a reply



Submit