This post shows you how to convert String to Double in Java. Let's write a generic code in this post.
Create Generic Converter interface
/**
* Converts objects of S type to T type.
*/
public interface Converter<S, T> {
/**
* Converts the source object from S type to T type.
*
* @param source the object to convert
*
* @return the converted object
*/
T convert(S source);
}
Converts String to Double - StringToDoubleConverter.java
Let's implement above Converter interface to convert String to Double in Java
/**
* Converts String to Double.
*
*/
public class StringToDoubleConverter implements Converter<String, Double> {
@Override
public Double convert(String source) {
return Double.valueOf(source);
}
}
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course