🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Converting a String to a Double in Java is a common task that can be useful in various scenarios, such as parsing user input, reading data from files, or handling configuration parameters. In Java, this conversion can be done using several methods. This blog post will explore different methods to convert a String to a Double in Java.
Table of Contents
- Using
Double.parseDouble() - Using
Double.valueOf() - Using
DecimalFormat - Complete Example Program
- Conclusion
1. Using Double.parseDouble()
The Double.parseDouble() method parses the string argument as a double.
Example:
public class StringToDoubleUsingParseDouble {
public static void main(String[] args) {
String strValue = "123.45";
// Convert string to double using Double.parseDouble()
double doubleValue = Double.parseDouble(strValue);
System.out.println("String value: " + strValue);
System.out.println("Double value: " + doubleValue);
}
}
Output:
String value: 123.45
Double value: 123.45
Explanation:
Double.parseDouble(strValue)converts the string value to its double representation.
2. Using Double.valueOf()
The Double.valueOf() method returns a Double instance representing the specified string value. This method can also be used to convert a String to a double.
Example:
public class StringToDoubleUsingValueOf {
public static void main(String[] args) {
String strValue = "678.90";
// Convert string to double using Double.valueOf()
double doubleValue = Double.valueOf(strValue);
System.out.println("String value: " + strValue);
System.out.println("Double value: " + doubleValue);
}
}
Output:
String value: 678.90
Double value: 678.9
Explanation:
Double.valueOf(strValue)converts the string value to its double representation.
3. Using DecimalFormat
The DecimalFormat class allows you to parse a string into a double using a specific pattern.
Example:
import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToDoubleUsingDecimalFormat {
public static void main(String[] args) {
String strValue = "1234.56";
// Convert string to double using DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#.##");
try {
double doubleValue = decimalFormat.parse(strValue).doubleValue();
System.out.println("String value: " + strValue);
System.out.println("Double value: " + doubleValue);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:
String value: 1234.56
Double value: 1234.56
Explanation:
decimalFormat.parse(strValue).doubleValue()converts the string value to its double representation using the specified pattern.
4. Complete Example Program
Here is a complete program that demonstrates all the methods discussed above to convert a String to a Double.
Example Code:
import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToDoubleExample {
public static void main(String[] args) {
String strValue1 = "123.45";
String strValue2 = "678.90";
String strValue3 = "1234.56";
// Using Double.parseDouble() Method
double doubleValue1 = Double.parseDouble(strValue1);
System.out.println("Using Double.parseDouble():");
System.out.println("String value: " + strValue1 + " -> Double value: " + doubleValue1);
// Using Double.valueOf() Method
double doubleValue2 = Double.valueOf(strValue2);
System.out.println("\nUsing Double.valueOf():");
System.out.println("String value: " + strValue2 + " -> Double value: " + doubleValue2);
// Using DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#.##");
try {
double doubleValue3 = decimalFormat.parse(strValue3).doubleValue();
System.out.println("\nUsing DecimalFormat:");
System.out.println("String value: " + strValue3 + " -> Double value: " + doubleValue3);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:
Using Double.parseDouble():
String value: 123.45 -> Double value: 123.45
Using Double.valueOf():
String value: 678.90 -> Double value: 678.9
Using DecimalFormat:
String value: 1234.56 -> Double value: 1234.56
5. Conclusion
Converting a String to a Double in Java can be accomplished in several ways. The Double.parseDouble() and Double.valueOf() methods are both straightforward and widely used. Using DecimalFormat provides additional control over the formatting of the double value. By understanding these different methods, you can choose the one that best fits your needs and coding style.
Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment