Core JAVA

Convert Double to String to Double in Java with Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Convert Double to String to Double in Java" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;String-to-Double-to-String&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>In my earlier tutorial we have seen how to<b> <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-string-to-integer-to-string-in-java-with-example&sol;">Convert Integer to String in Java<&sol;a><&sol;b>&period; In this tutorial we will see how to convert Double to String and its vice versa&period; In Java application many times we need to convert a Double to String and its vice versa&period; From Java 5&comma; Java was introduced new feature Auto-boxing which automatically converts primitive type to Object type&period; Means double automatically converted to Double object&period; So we can also convert String to double and double to String object&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s see the conversions for String to Double and Double to String<&sol;p>&NewLine;<h2><b>Converting String to Double in Java<&sol;b><&sol;h2>&NewLine;<p>In java there are more ways to converting String to Double object or double value as per as our need&period; So let&&num;8217&semi;s discuss some frequently used ways for this conversion&period;<&sol;p>&NewLine;<p><b>1&period;<&sol;b> If you want to convert String value to Double value then simply you could create the new object of Double class&period; Double class has number of constructors but it has one constructor which expects a String value and it return a Double object with same value&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">String doubleStr &equals; "99&period;9"&semi; &NewLine;Double doubleObj &equals; new Double&lpar;doubleStr&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>But <b>doubleStr <&sol;b>is not representing a valid number then it will occur <i><b>NumberFormatException<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p><b>2&period;<&sol;b> Java has another way to convert String object to Double object by using <i><b>valueOf&lpar;&rpar;<&sol;b><&sol;i> static method of Double class&period; This method return Double object&period; You just pass your double string into this method and it will convert to equivalent Double value&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">String doubleStr &equals; "99&period;9"&semi; &NewLine;Double doubleObj &equals; Double&period;valueOf&lpar;doubleStr&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>This method can also throw <i><b>NumberFormatException <&sol;b><&sol;i>if String is null or not valid to double&period; Above two conversions always give us Double object but if want exact double value instead of Double object then try with following another way&period;<&sol;p>&NewLine;<p><b>3&period;<&sol;b> If you want to convert your string to double value as primitive type then you could this way&period; According this way of String to double conversion is by using <i><b>parseDouble&lpar;String str&rpar;<&sol;b> <&sol;i>from Double class&period; And also this recommended way to converting String to double if want simple value because it&&num;8217&semi;s more readable and standard way of converting a string value to double&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">String doubleStr &equals; "99&period;9"&semi; &NewLine;double doubleVal &equals; Double&period;parseDouble&lpar;doubleStr&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>This is static method of Double class and it will throw <b><i>NumberFormatException <&sol;i><&sol;b>if <b>doubleStr <&sol;b>is not representing a valid number&period;<&sol;p>&NewLine;<h2><b>Converting Double to String in Java<&sol;b><&sol;h2>&NewLine;<p>As above conversion we have seen String to Double&comma; here we will see number ways to convert Double to String object&period; In the example we are converting double to String&period; Let&&num;8217&semi;s see below points<br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>1&period;<&sol;b> It very easiest way to converting double value to String value by using concatenation with empty string which produce a new string&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Double doubleObj &equals; 99&period;9&semi; &NewLine;String doubleStr &equals; "" &plus; doubleObj&semi; &NewLine;<&sol;pre>&NewLine;<p><b>2&period;<&sol;b> String class has number of static methods&period; One of them is <b><i>valueOf&lpar;&rpar;<&sol;i><&sol;b> method&period; We can also use this method for converting double to String&period; This method takes a double value as an argument and returns it in a form of String literal&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Double doubleObj &equals; 99&period;9&semi; &NewLine;String doubleStr &equals; String&period;valueOf&lpar;doubleObj&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><b>3&period;<&sol;b> Double class has one of method toString&lpar;&rpar;&period; Double class overrides this method of Object class&period; This is also one of very simple way to convert double into String by using toString&lpar;&rpar; method of Double Class&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Double doubleObj &equals; 99&period;9&semi; &NewLine;String doubleStr &equals; doubleObj&period;toString&lpar;&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p><b>4&period;<&sol;b> We can also use formatter of String class&period; In the String class there are lot of String&period;format&lpar;&rpar; overloaded methods&period; We can use one of them for conversion of double to String&period; This way is rather more a flexible way of getting String from Double&period; it uses String&period;format&lpar;&rpar; method and returns a formatted string so you can control the precision level and get a String up to two decimal points or three decimal points based on your requirement&period;<&sol;p>&NewLine;<p><b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">Double doubleObj &equals; 99&period;99999&semi; &NewLine;String doubleStr &equals; String&period;format&lpar;"&percnt;&period;2f"&comma; doubleObj&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>This method return formatted string value which contains double value up to 2 decimal points&period; Here &&num;8220&semi;f&&num;8221&semi; is used to format floating point numbers&period;<&sol;p>&NewLine;<p><b>Summary<&sol;b><br &sol;>&NewLine;We have seen different ways to converting String to Double and its vice versa&period; I always prefer parseDouble&lpar;&rpar; method of Double class for converting String to double primitive type&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<p><b>String Related Posts<&sol;b><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-make-java-class-immutable&sol;">How to make Immutable Class in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-interview-questions&sol;">Core Java Interview Questions<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;string-class-in-java&sol;">String Class in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;why-string-is-immutable-and-final-in-java&sol;">Why String is Immutable and Final in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-string-to-integer-to-string-in-java-with-example&sol;">Convert String to Integer to String in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-double-to-string-to-double-in-java-with-example&sol;">Convert Double to String to Double in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-date-to-string-in-java-with-example&sol;">Convert Date to String in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;differences-between-string-stringbuffer-and-stringbuilder-in-java&sol;">Differences between String&comma; StringBuffer and StringBuilder in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-stringbuilder-and-stringbuffer-in-java&sol;">Differences StringBuffer and StringBuilder<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-compare-two-strings-in-java&sol;">How to Compare two Strings in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashcode-and-equals-methods-in-java&sol;">Difference between hashCode&lpar;&rpar; and equals&lpar;&rpar; methods in Java<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-string-substring-method&sol;"> Java &&num;8211&semi; String substring&lpar;&rpar; Method<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-wait-and-sleep&sol;"> Difference between wait&lpar;&rpar; and sleep&lpar;&rpar;<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-compare-two-strings-in-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-date-to-string-in-java-with-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '67'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago