1. Introduction

Converting numeric values to strings is a fundamental operation in programming. While doubles efficiently handle a wide range of values, converting them to string format may result in scientific notation, which compromises readability.

In this tutorial, we’ll explore different techniques in Kotlin to convert double values to string representations without resorting to scientific notation.

2. About Scientific Notation

Scientific notation is the standard format for representing floating-point numbers using an exponent denoted by E or e. This notation expresses numbers as a base raised to a power of 10. For example, we can represent the number 12345 as 1.2345E4.

Similarly, the decimal number 0.012345 can be expressed in scientific notation as 1.23450E-02 using the negative exponent of 10.

3. Using String.format()

We can use the format() method from the String class to convert the decimal value to a string format without scientific notation. Let’s look at the code:

val num = 0.000123456789 
println(num) // prints 1.23456789E-4
val numStr = String.format(Locale.US, "%.8f", num)
println(num) // prints 0.00012346

Here, we use the US locale to use a dot as the decimal separator. In some locales, like German, a comma is used instead.

Kotlin provides an extension function on the String class to make this easier. We can rewrite the above code:

"%.8f".format(Locale.US, num)

This function behaves identically to the String.format() method.

However, we should note that we should be careful to use a correct format string, otherwise we might lose precision.

4. Using NumberFormat.format()

An alternative approach to perform the conversion is by utilizing the NumberFormat class:

val num = 0.000123456789
val numberFormat = NumberFormat.getInstance(Locale.US)
numberFormat.maximumFractionDigits = 8
numberFormat.format(num) //0.00012346 

In this case, we used the format() method from NumberFormat to convert the number to a string representation, specifying a precision of 8 decimal points. Similar to the previous example, we applied the US locale to the NumberFormat instance.

5. Using DecimalFormat.format()

Another way to format the number is to use the DecimalFormat class from Java. DecimalFormat is a subclass of NumberFormat designed specifically for formatting decimal numbers. Unlike NumberFormat, a general-purpose formatting class supporting various formats like numbers and currencies, DecimalFormat provides finer control over formatting details such as decimal places, separators, symbols, and more.

Let’s look at how we can use DecimalFormat to convert our double to string format:

val num = 0.000123456789
val symbols = DecimalFormatSymbols(Locale.US)
val df = DecimalFormat("#.###############", symbols)
df.format(num) //0.000123456789

In this case, we provided the pattern string to the DecimalFormat class. Additionally, we can configure various formatting options by utilizing the DecimalFormatSymbols class.

6. Using BigDecimal.toPlainString()

We can also use a BigDecimal method to convert a numeric value to string representation:

val num = 0.000123456789
BigDecimal.valueOf(num).toPlainString() //0.000123456789

In this scenario, we initially convert the Double into a BigDecimal instance. Subsequently, we utilize the toPlainString() method to convert it into a string value without scientific notation.

7. Conclusion

In this brief article, we explored various methods for converting a double value into a string representation without scientific notation. Depending on convenience and specific requirements, any of the methods discussed here can be utilized.

As always, the sample code used in this tutorial is available over on GitHub.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments