×

How to convert float to String in Java

How to convert float to String in java

There are following methods to convert float to String:

  • Convert using Float.toString(float)
  • Convert using String.valueOf(float)

Convert using Float.toString(float)

The Float class has a static method that returns a String object representing the specified float parameter. It returns the string representation of the float argument. The minus sign will be preserved if the number is negative. This is the most common method for converting the float value to String. The signature of this method is-

public static String toString(float f)

Where f represents the float value that we want to convert.

This method works similar to the String.valueOf(float) method except that it belongs to the Float class.

Example

In the following example, var is a variable of type float having value -7.22. str is a variable of type String that stores the value of var after conversion. Float is a wrapper class. toString() is a method of String class having var as an argument and returns the String representation of float value. The println statement prints the converted value of var.

public class FloatToStringExample1
{
public static void main(String[] args)
{
float var = -7.22f; //the suffix f denotes the float representation
String str = Float.toString(var); //conversion using to String() method
System.out.println("After Conversion String is:  "+str);
}
}

Output

After Conversion String is: -7.22

Convert using String.valueOf(float)

It is an overloaded method of String class under the java.lang package. You can pass the float value to this method as an argument. It returns the string representation of it. The signature of this method is-

public static String valueOf(float f)

Where f represents the float value that we want to convert.

Example

In the following example, val is a variable of type float having value 21.20. str is a variable of type String that stores the value of val after conversion. String is a class. valueOf() is a method of String class having val as an argument and returns the String representation of float value. The println statement prints the converted value of val.

public class FloatToStringExample2
{
public static void main(String[] args)
{
float val= 21.20f; //the suffix f denotes the float representation
String str = String.valueOf(val); //conversion using valueOf() method
System.out.println("After conversion String is: "+str);
}

Output

After conversion String is: 21.2

Related Topics

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.