Float to String conversion in Java – 6 ways

In this article, we will discuss various ways to convert Float to String in Java

 

What is the need of converting primitive float or Float wrapper-type to String ?

  • Generally, whenever we receive or send data from web application then it all passed in the form of String only
  • After mathematical calculation, all numbers need to be converted into String for displaying
  • This article explains about primitive float data-type or Float wrapper-type to String conversion only, but we can do conversion for other types like long, double, int, etc. to String object
  • Note: Likewise, sometimes String to Float conversion is also required

 

Various ways to convert Float to String

  1. using Float.toString(floatValue);
  2. using String.valueOf(floatValue);
  3. using String.format(type, floatValue);
  4. create Float object and then convert to String using toString() method {new Float(floatValue).toString();}
  5. adding double quotes (“”) to float value {i.e.; “” + floatValue;}
  6. using append() method of StringBuffer & StringBuilder

 

Auto-boxing and un-boxing feature from Java 1.5 version:

  • parseFloat() method returns primitive float data-type, but it can be easily used as Float wrapper-type
  • as auto-boxing feature helps to convert primitive data-type to wrapper-types
  • similarly, Float.valueOf() method returns Float wrapper-type, but it can also be used as primitive float
  • let’s see one example on this auto-boxing feature

Read String class in detail with example

 

Let us move forward and discuss all possible ways to convert Float to String in Java

Way 1: Using Float.toString(floatValue); method

  • This method can be used to convert primitive float data-type or Float wrapper-type to String object
  • Negative float data-type can also be converted to String type, as shown in the below example

Method signature:

public static String toString(float f);

ConvertFloatIntoStringUsingToStringMethod.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 545.45f;

		// 1. converting float to String by using toString()
		String str1 = Float.toString(fValue1);
		System.out.println("1. Converted"
				+ " primitive float to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -54.545f;

		// 2. converting float to String by using toString()
		String str2 = Float.toString(fValue2);
		System.out.println("\n2. Converted "
				+ "negative primitive float to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(123.45f);

		// 3. converting Float object to String 
		// by invoking toString()
		String str3 = Float.toString(fValue3);
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive float to String value is : 545.45

2. Converted negative primitive float to String value is : -54.545

3. Converted Float object to String value is : 123.45

Final concatenated floating strings : 
545.45 -54.545 123.45

 

Way 2: Using String.valueOf(floatValue); method

  • This method can be used to convert primitive float data-type or Float wrapper-type to String object
  • Negative integer data-type can also be converted to String type, as shown in the below example

Method signature:

public static String valueOf(float f);

ConvertFloatIntoStringUsingValueOfMethod.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 989.89f;

		// 1. converting float to String by using valueOf()
		String str1 = String.valueOf(fValue1);
		System.out.println("1. Converted"
				+ " primitive float to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -98.989f;

		// 2. converting negative float to String 
		// by using valueOf()
		String str2 = String.valueOf(fValue2);
		System.out.println("\n2. Converted "
				+ "negative primitive float to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(123.45f);

		// 3. converting Float object to String by using valueOf()
		String str3 = String.valueOf(fValue3);
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive float to String value is : 989.89

2. Converted negative primitive float to String value is : -98.989

3. Converted Float object to String value is : 123.45

Final concatenated floating strings : 
989.89 -98.989 123.45

 

Way 3: Using String.format(type, number); method

  • This method can be used to convert primitive float data-type or Float wrapper-type to String object
  • Negative integer data-type can also be converted to String type, as shown in the below example

Method signature:

public static String format(String format, Object... args);

ConvertFloatIntoStringUsingFormatMethod.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringUsingFormatMethod {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 6.6442f;

		// 1. converting float to String by using format()
		String str1 = String.format("%f", fValue1);
		System.out.println("1. Converted"
				+ " primitive int to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -6.6472f;

		// 2. converting negative float to String 
		// by using format()
		String str2 = String.format("%.2f", fValue2);
		System.out.println("\n2. Converted "
				+ "negative primitive int to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(54.321f);

		// 3. converting Float object to String 
		// by using format() method
		String str3 = String.format("%.5f", fValue3);
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 6.644200

2. Converted negative primitive int to String value is : -6.65

3. Converted Float object to String value is : 54.32100

Final concatenated floating strings : 
6.644200 -6.65 54.32100

 

Way 4: create Float object and then convert to String invoking toString() method

  • Here, we will create new Float object with primitive float value as constructor-argument
  • After creating new Float object by passing primitive float value, then invoke toString() method for converting primitive float data-type to String object

Method signature:

public Float(float value);

public String toString();

ConvertFloatIntoStringUsingObjectCreation.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 87.5421f;

		// 1. converting float to String 
		// by creating Integer object 
		Float floatObj1 = new Float(fValue1);

		// and then invoke toString(); method
		String str1 = floatObj1.toString();
		System.out.println("1. Converted"
				+ " primitive float to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -8754.21f;

		// 2. converting negative int to String 
		// by creating Integer object
		Float floatObj2 = new Float(fValue2);

		// and then invoke toString(); method
		String str2 = floatObj2.toString();
		System.out.println("\n2. Converted "
				+ "negative primitive float to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(1.4785f);

		// 3. converting Float object to String 
		// by creating Float object
		String str3 = fValue3.toString();
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive float to String value is : 87.5421

2. Converted negative primitive float to String value is : -8754.21

3. Converted Float object to String value is : 1.4785

Final concatenated floating strings : 
87.5421 -8754.21 1.4785

 

Way 5: adding double quotes (“”) to float value

  • Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + floatValue;

ConvertFloatIntoStringByAddingDoubleQuotes.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 98.5632f;

		// 1. converting float to String by adding ""
		String str1 = "" + fValue1;
		System.out.println("1. Converted"
				+ " primitive float to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -9856.32f;

		// 2. converting negative float to String  by adding ""
		String str2 = "" + fValue2;
		System.out.println("\n2. Converted "
				+ "negative primitive float to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(5.4321f);

		// 3. converting Integer object to String  by adding ""
		String str3 = "" + fValue3;
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive float to String value is : 98.5632

2. Converted negative primitive float to String value is : -9856.32

3. Converted Float object to String value is : 5.4321

Final concatenated floating strings : 
98.5632 -9856.32 5.4321

 

Way 6: Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive float data-type to String object
  • Only sign is allowed, like minus (-)

Syntax:

StringBuffer sb = sb.append(floatValue);

String temp = sb.toString();

ConvertFloatIntoStringByAppending.java

package in.bench.resources.floating.to.string.conversion;

public class ConvertFloatIntoStringByAppending {

	public static void main(String[] args) {

		// primitive float data-type
		float fValue1 = 21.3659f;

		// 1. converting float to String 
		// by using append() method
		StringBuffer sb1 = new StringBuffer();
		String str1 = sb1.append(fValue1).toString();
		System.out.println("1. Converted"
				+ " primitive float to String value is : "
				+ str1);


		// primitive float data-type (negative value)
		float fValue2 = -2136.59f;

		// 2. converting negative float to String 
		// by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(fValue2).toString();
		System.out.println("\n2. Converted "
				+ "negative primitive float to String value is : "
				+ str2);


		// Float object
		Float fValue3 = new Float(867.514f);

		// 3. converting Float object to String 
		// by using append() method
		StringBuffer sb3 = new StringBuffer();
		String str3 = sb3.append(fValue3).toString();
		System.out.println("\n3. Converted"
				+ " Float object to String value is : "
				+ str3);


		// converted and concatenated strings using + operator
		System.out.println("\nFinal concatenated"
				+ " floating strings : ");
		System.out.println(str1 + " " + str2 + " " + str3);
	}
}

Output:

1. Converted primitive float to String value is : 21.3659

2. Converted negative primitive float to String value is : -2136.59

3. Converted Float object to String value is : 867.514

Final concatenated floating strings : 
21.3659 -2136.59 867.514

 

What if we want to convert to Float wrapper-type to primitive float data-type and vice-versa ?

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing and un-boxing feature

AutoBoxingFeatureForFloatConversion.java

package in.bench.resources.string.to.floating.conversion;

public class AutoBoxingFeatureForFloatConversion {

	public static void main(String[] args) {

		// String with floating numbers or digits
		String str1 = "12.345";

		// string to float conversion
		float fValue1 = Float.parseFloat(str1);

		// 1. Auto-Boxing - converting float to Float
		Float fAutoBoxing = fValue1;
		System.out.println("1. Auto-Boxing : "
				+ fAutoBoxing);

		// String with floating number/digits and minus sign (-)
		String str2 = "-98.765";

		// string to Float conversion
		Float fValue2 = Float.valueOf(str2);

		// 2. Un-Boxing - converting Float to float
		float fUnBoxing = fValue2;
		System.out.println("\n2. Un-Boxing   : "
				+ fUnBoxing);
	}
}

Output:

1. Auto-Boxing : 12.345

2. Un-Boxing   : -98.765

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

String to double conversion in Java - 3 ways
String to float conversion in Java - 3 ways