×

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

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

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.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long. Syntax: public static int incrementExact (int a)public static...

1 minute read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 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.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.