Explain Codes LogoExplain Codes Logo

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

java
formatting
precision
decimalformat
Nikita BarsukovbyNikita Barsukov·Dec 30, 2024
TLDR

In Java, the DecimalFormat class can be used to print a double without scientific notation:

double value = 1234.56; System.out.println(new DecimalFormat("0.

######").format(value));


This command will print out **`1234.56`**, using the provided format and omitting scientific notation. Customize the format pattern `"0.######"` according to the precision you require.

## Fine-tuning double formatting

### Employing DecimalFormat for precision control

If you need to set an exact number of **decimal places** while printing double values, `DecimalFormat` is the tool to use:

```java
double pi = Math.PI; 
DecimalFormat decimalFormat = new DecimalFormat("#.00000"); 
System.out.println(decimalFormat.format(pi)); //Slices Up Pi: 3.14159

When presenting data across various locales, DecimalFormat can be attuned to considerations of locale:

DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH); decimalFormat.setDecimalFormatSymbols(symbols); //Channeling some inner English

Exercising BigDecimal for extreme numbers

For very large or small numbers, BigDecimal becomes super handy. Its toPlainString() method eliminates scientific notation:

double value = 0.00000000002; BigDecimal bigDecimal = new BigDecimal(String.valueOf(value)); System.out.println(bigDecimal.toPlainString()); //Infinity War Avenger: 0.00000000002

For maintaining accuracy, create BigDecimal instances from a String version of the double, not the double itself:

double value = 0.0000003; BigDecimal exactBigDecimal = new BigDecimal(Double.toString(value)); System.out.println(exactBigDecimal.toPlainString()); //Not the droid you're looking for: 0.0000003

Controlling precision using printf

For folks who prefer printf for formatting, Java provides an easy method to regulate the precision:

double value = 3.14159; System.out.printf("%.2f", value); //Just two decimals, because who needs accuracy?

To specify the number of digits after the decimal point, simply change the .2 in %.2f.

#####"); System.out.println(formatter.format(123456.789)); // Precision nailing: 123456.789

System.out.println(String.format("%.5f", 123456.789)); // Math Attack: 123456.78900


**No** rocket science, just **running** with precision!

## Advanced double formatting techniques

### Tackling special cases

Before converting a **double** to a string using `println`, ensure you catch special floating-point values:

```java
double notANumber = Double.NaN;
double infinity = Double.POSITIVE_INFINITY;
if (Double.isNaN(notANumber) || Double.isInfinite(infinity)) {
    System.out.println("Hold your unicorns! We've got a special one here.");
}

Considering locale effects on DecimalFormat

While using DecimalFormat, remain cognizant of your locale as formats can differ:

DecimalFormat decimalFormat = new DecimalFormat("#.00"); decimalFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.FRENCH)); System.out.println(decimalFormat.format(123456.789)); // Bonjour, comma: 123 456,79!

Even though double is an excellent datatype for floating-point arithmetic, it has its limits and may not hold the exact value due to binary representation:

System.out.println(0.1 + 0.2); // Math is hard: 0.30000000000000004

When precision is crucial, consider using BigDecimal.