Introduction :
In Java, we can convert a double
value to a string
by using the String.valueOf()
inbuilt function. It will give the same desired output, i.e. we will get the same string value as the input double value. But, it may return a string with an exponential component or in scientific notation. Let me show you an example:
public class Main {
public static void main(String[] args) {
double number1 = 12;
double number2 = 0.054;
double number3 = 0.000047823458;
System.out.println(String.valueOf(number1));
System.out.println(String.valueOf(number2));
System.out.println(String.valueOf(number3));
}
}
Output :
12.0
0.054
4.7823458E-5
As you can see, it works for the first and the second number. But for the last number, it returned one exponential value.
Suppose your program sending the double numbers to the backend in string format. The server is using the values to do some other calculations. So, if the value contains exponential components, it may not work if it is not handled in the backend. This is just a simple example that came to my mind. We may have different problems similar to this one if the values are not formatted properly.
In this tutorial, I will show you how to convert a double
value to a String
in Java without the exponential part. There are multiple ways to do the conversion. Let me show you one by one :
By using the String.format()
method:
We can format a double number with the String.format()
method. It takes two arguments, the first one is the format string and the second one is one or more objects that we are formatting.
public class Main {
public static void main(String[] args) {
double number = 12.000047823458;
System.out.println(String.format("%f",number));
System.out.println(String.format("%.0f",number));
System.out.println(String.format("%.12f",number));
}
}
Download it on Github
In this example, we have used three different ways to format the same double value. It will print the below output:
12.000048
12
12.000047823458
As you can see, the double
number is formatted to a String
completely only for the format string %.12f
as there are 12 digits after the decimal. This method is useful if you know how many decimal places are there after the decimal point or you can define a maximum number of decimal places to consider.
By using the Double.toString()
method:
The Double
class comes with an inbuilt method toString()
to convert a doubl
e value to a String
. It will not add any exponential component to the conversion.
public class Main {
public static void main(String[] args) {
double number = 12.000047823458;
System.out.println(Double.toString(number));
}
}
Download it on Github
Output :
The above program will print the below output:
12.000047823458
By adding an empty string:
This is the simplest way to convert a double
value to a String
. We need to add one empty string to the double
value and it will be converted to a string. For example:
public class Main {
public static void main(String[] args) {
double number = 12.000047823458;
String numberInString = number + "";
System.out.println(numberInString);
}
}
Download it on Github
By using the DecimalFormat
class:
We can create one object of the DecimalFormat
class with one pattern, and based on that pattern, it formats the input number and returns the string representation of the formatted number. The format()
method can be used to format a number. For example:
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
double number = 12.000047823458;
String pattern = "##.############";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
System.out.println(decimalFormat.format(number));
}
}
Download it on Github
It will print the same output as the previous examples, but you need to provide the exact decimal values you need in the final result.
The DecimalFormat
class is useful if we want to format a double
value to a specific format e.g. the pattern ##.##
will format it up to two values after the decimal.
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
double number = 12.164547823458;
String pattern = "##.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
System.out.println(decimalFormat.format(number));
}
}
It will print 12.16
as the output.
Conclusion :
This tutorial explained different ways to convert double
to String
without an exponential part. You can use any of the above methods. You can fork the project on Github and please raise a pull request if I missed anything.