Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


1. Introduction

In our daily tasks, we may often come across challenges to format double values. One such challenge could be printing a double value, avoiding scientific notation. Even though this method helps us express large and small values more compactly, there are situations where the default scientific notation might not be the most practical choice. In such cases, alternative approaches may need to be considered for a more suitable representation.

In this tutorial, we’ll explore various methods to print a double value without scientific notation in Java.

2. About Scientific Notation

Scientific notation consists of two components: a coefficient and an exponent. Typically, a decimal number between 1 and 10 is the coefficient, while the exponent indicates the power of 10 by which the system multiplies the coefficient.

In Java, scientific notation is often represented using the “e” notation, where “e” stands for exponent:

double largeNumber = 256450000d;
System.out.println("Large Number: " + largeNumber);

double smallNumber = 0.0000046d;
System.out.println("Small Number: " + smallNumber);

Running the code from above will output to the console two scientifically notated numbers:

Large Number: 2.5645E8
Small Number: 4.6E-6

The first number was represented as 2.5645 * 108, and the second one as 4.6 * 10-6 using the “e” notation. This compact writing can help us many times, but it can also mislead us. Due to this, in the following, we’ll see some methods to eliminate it.

3. Using the DecimalFormat Class

We can easily control how numeric values are shown using the DecimalFormat class. Also, it allows us to specify formatting patterns. We can define the desired decimal places and other formatting details to suit our application’s requirements:

DecimalFormat df = new DecimalFormat("#.###########");
double largeNumber = 256450000d;
System.out.println("Large Number: " + df.format(largeNumber));
double smallNumber = 0.0000046d;
System.out.println("Small Number: " + df.format(smallNumber));

We created a pattern that consists of a sequence of special characters, possibly combined with text. In our examples, we opted for the ‘#’ character, which displays a digit when one is provided and nothing otherwise. If we run the code from above, we will print the numbers on the console without scientific notation:

Large Number: 256450000
Small Number: 0.0000046

We can see that even if we put only one character for the integer part, it will always be retained, regardless of whether the pattern is smaller than the actual number.

4. Using printf() Method

The printf() method in the PrintStream class offers a dynamic and straightforward way to format output. It closely resembles the traditional C-style printf() function.

We’ll use the %f specifier to shape the presentation of floating-point numbers assertively:

double largeNumber = 256450000d;
System.out.printf("Large Number: %.7f", largeNumber);
double smallNumber = 0.0000046d;
System.out.printf("Small Number: %.7f", smallNumber);

In this case, we’ll output:

Large Number: 256450000.0000000
Small Number: 0.0000046

If the number has no decimals, it will fill the decimal places with zeros. Also, if we don’t specify the number of decimals, it will print, by default, six decimal places.

If we print our small number without explicitly specifying the number of decimals:

System.out.printf("Small Number: %f", smallNumber);

Because the value has more decimals than specified in the format, the printf() method will round the value:

Small Number: 0.000005

5. Using BigDecimal

BigDecimal is ideal when dealing with financial calculations, scientific computations, or any application where maintaining precision is crucial. Unlike primitive data types or double values, which can lead to precision loss due to the inherent limitations of binary floating-point representation, BigDecimal allows us to specify the exact precision we need.

When working with BigDecimal, the concept of scientific notation is inherently different. The class itself allows us to work with arbitrary precision numbers without resorting to scientific notation, providing a straightforward solution to the challenges posed by default representations:

double largeNumber = 256450000d;
System.out.println("Large Number: " + BigDecimal.valueOf(largeNumber).toPlainString());
double smallNumber = 0.0000046d;
System.out.println("Small Number: " + BigDecimal.valueOf(smallNumber).toPlainString());

In this example, we employ the BigDecimal.valueOf() method to convert a double value with scientific notation into a BigDecimal. Subsequently, the toPlainString() function converts our complex object into a printable string, providing a precise and easily interpretable representation.

Besides eliminating scientific notation, BigDecimal provides features such as customizable rounding modes, which can be crucial in financial applications. The ability to precisely control the number of decimal places and handle extremely large or small numbers makes BigDecimal a go-to choice for developers prioritizing accuracy and precision in their Java applications.

6. Using String.format()

The String.format() method shapes a String using a format string and arguments.

Both String.format() and printf() methods use the java.util.Formatter class and share the exact underlying mechanism for formatting, providing a convenient way to produce well-structured output. However, there are subtle differences in their application and usage.

double largeNumber = 256450000d;
String formattedLargeNumber = String.format("%.7f", largeNumber);
System.out.println("Large Number: " + formattedLargeNumber);
double smallNumber = 0.0000046d;
String formattedSmallNumber = String.format("%.7f", smallNumber);
System.out.println("Small Number: " + formattedSmallNumber);

As can be seen in the example above, the String.format() method returns a formatted string rather than directly printing it to the console. We can store the formatted string in a variable, therefore making it easier for us to handle it further or use it in different contexts. Both methods share the same format specifiers, so the syntax for formatting remains consistent between them.

7. Conclusion

In this article, we saw multiple avenues for avoiding scientific notation when printing double values. Even more, we’ve seen many ways to format numbers, small or large, using different methods. We also played with format specifiers to ensure our desired level of detail.

Ultimately, the choice between methods rests on personal preference and the specific needs of the task. As always, the complete source code for the examples is available over on GitHub.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


res – REST with Spring (eBook) (everywhere)
Inline Feedbacks
View all comments