Java StringToFloatConverter

This post shows you how to convert String to Float 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 Float - StringToFloatConverter.java

Let's implement above Converter interface to convert String to Float in Java
/**
 * Converts String to Float.
 */
public class StringToFloatConverter implements Converter<String, Float> {

    @Override
    public Float convert(String source) {
        return Float.valueOf(source);
    }
}

Comments