Home > Java > javaTutorial > Java String to Float

Java String to Float

王林
Release: 2024-08-30 15:18:10
Original
335 people have browsed it

String to float conversion is generally used if we have to perform some mathematical calculations on string data that contains float type numbers. There are situations in which we read data in the form of a string, and we need to convert it into float to perform required operations. In these situations, we need to use Java API’s to convert string types to float.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Convert String to Float in Java?

There are mainly four different ways to convert string to float in java:

  • Using Float.valueOf(): This is a built-in static method of Float class that accepts a given string which is to be converted into float and returns a float value corresponding to the passed input string. This method can throw NumberFormatException if the passed string is not a valid number and NullPointerException if null is passed as input. This returns a primitive object of the Float class.
  • Using Float.parseFloat(): This is also a built-in static method of Float class which accepts a given string that is to be converted into float and returns a float value corresponding to the passed input string. This returns a primitive float value, unlike the valueOf () method, which returns a Float object.
  • Using Constructor: This simply involves using the Float class construct that accepts string parameter as input and returns a float.
  • Using java.text.DecimalFormat Class: This method involves creating an instance of java.text.DecimalFormat class, calling parse method of DecimalFormat class which accepts the string to be converted into a float as input and then followed by calling method floatValue to get float value corresponding to the given string.
  • Based on our requirement, any one of the above four mentioned ways can be used to convert float to string.

Syntax

1. Float.valueOf ()

public static Float valueOf(String input) throws NumberFormatException
Copy after login

In the above syntax, input is the string that is to be converted to float. The return type of the above method is an object of the Float wrapper class. 

2. Float.parseFloat()

public static float parseFloat(String input) throws NumberFormatException
Copy after login

In the above syntax, input is the string that is to be converted to float. The return type of the above method is a float value.

3. Using constructor

new Float(String input)
Copy after login

Calling the above constructor with string to be converted to float as input gives float value corresponding to the given string.

4. text.DecimalFormat

DecimalFormat format= new DecimalFormat (“#”);
format.parse(String input).floatValue();
Copy after login

In the above method, first an instance of java.text.DecimalFormat is created, and then the parse method is involved accepting string as input type, then followed by the floatValue () method to get float value.

Note: All the above methods may throw an exception in case an invalid string is passed as input.

Examples of Java String to Float

Below are the various examples of the Java string to float:

Example #1

This example shows how to use the parseFloat() method to convert string to float.

Code:

public class StringToFloatDemo{
public Float convertStringToFloat(String stringvalue){
try{
float floatValue= Float.parseFloat(stringvalue);
return floatValue;
}
catch(NumberFormatException e){
System.out.println ("NumberFormatException occurred.");
System.out.println(stringvalue + " is not a valid number.");
return null;
}
}
public static void main(String args[])
{
StringToFloatDemo demo = new StringToFloatDemo();
String input= "201.5";
float output= demo.convertStringToFloat("201.50");
System.out.println("Float Value of " + input + " is : " + output);
}
}
Copy after login

Output:

Java String to Float

In case of an exception, let us consider if “Edubca” is passed as input to convertStringToFloat function, then it will show the following output:

Java String to Float

Example #2

In this example, we will show how to use the constructor approach to convert string to float.

Code:

public class StringToFloatDemo{
public Float convertStringToFloat(String stringvalue){
try{
float floatValue = new Float(stringvalue);
return floatValue;
}
catch(NumberFormatException e){
System.out.println ("NumberFormatException occurred.");
System.out.println(stringvalue + " is not a valid number.");
return null;
}
}
public static void main(String args[])
{
StringToFloatDemo demo = new StringToFloatDemo();
String input= "200";
float output= demo.convertStringToFloat(input);
System.out.println("Float Value of " + input + " is : " + output);
}
}
Copy after login

Output:

Java String to Float

Similar to example 1, if an invalid string is passed as input, then NumberFormatException will be generated.

Example #3

In this example, we will show how to use the valueOf() method of the Float class to convert string to float.

Code:

public class StringToFloatDemo{
public Float convertStringToFloat(String stringvalue){
float floatValue;
try{
floatValue= Float.valueOf(stringvalue);
return floatValue;
}catch (NumberFormatException e)
{
System.out.println ("NumberFormatException occurred.");
System.out.println(stringvalue + " is not a valid number.");
return null;
}
}
public static void main(String args[])
{
StringToFloatDemo demo = new StringToFloatDemo();
String input= "200";
float output= demo.convertStringToFloat(input);
System.out.println("Float Value of " + input + " is : " + output);
}
}
Copy after login

Output:

Java String to Float

Similar to example 1 and 2, if an invalid string is passed as input, then NumberFormatException will be generated.

Example #4

In this example, we will show how to use the DecimalFormat class to convert string to float.

Code:

import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToFloatDemo{
public Float convertStringToFloat(String stringvalue){
float floatValue;
DecimalFormat decimalFormat = new DecimalFormat("#");
try {
floatValue = decimalFormat.parse(stringvalue).floatValue();
return floatValue;
} catch (ParseException e) {
System.out.println("Parse Exception Occurred");
System.out.println(stringvalue + " is not a valid number.");
return null;
}
}
public static void main(String args[])
{
StringToFloatDemo demo = new StringToFloatDemo();
String input= "200";
float output= demo.convertStringToFloat(input);
System.out.println("Float Value of " + input + " is : " + output);
}
}
Copy after login

Output:

Java String to Float

In case an invalid String is passed as input, then Parse Exception will be generated.

Let us consider the case when we pass “Edubca” instead of 200 as input to the above program; then the below error will be generated:

Java String to Float

The above is the detailed content of Java String to Float. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template