How to convert String to Float in Java?

In Java understanding of all data types conversion is helpful in many ways. In this post, we are going to learn how to covert string to float in java

Consider the scenario, if you are receiving float value using text field or text area, the float value is stored in the form of string. Then at that place there is a need to convert string to float for further operations.

parseFloat(String s)

It is used to convert a float value stored in a string to a float variable. It is a static method of float class.

public static float parseFloat(String s) throws NumberFormatException

Parameters

s - a string to be parsed

Returns

returns a float value

Exception

if a string cannot be parsed throws NumberFormatException

valueOf(String s)

It is also one of static method of float class. If string is null, throws NullPointer Exception

public static float valueOf(String s) throws NumberFormatException

Parameters

s - a string to be parsed

Returns

returns a float value

Exception

if a string cannot be parsed throws NumberFormatException

Below code contains conversions of string to float using above two methods


Related Posts

Click here for all data type conversions in Java

 Please let us know your views in comment section.

Subscribe us to receive updates instantly.

Happy Coding.!!!





Comments

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II