Java Float Primitive Data Type With Program Example

For decimal representation, Java provides a separate category named floating-point types that includes float and double.

In this article, we’ll be discussing float data type.

In Java, a float is a 32-bit primitive type for storing fractional values. It represents single-precision values that take half-space than double precision. This data type is useful for fractional values with less degree of precision i.e division, calculating roots, and trigonometry. Its default values is 0.0f.

Syntax: 

float floatNum = 43.4f;

Java float example

Float is also a signed type that is capable of storing positive and negative numbers. Below is the program to demonstrate the same.

public class FloatExample 
{
    
public static void main(String[] args) {
 
      float floatNum = 43.4f;
  
      float negativeNum = -43.4f;

 
      System.out.println("float value +ve :: "+ floatNum);
        
      System.out.println("float value -ve :: "+ negativeNum);

        
      System.out.println("adding both :: "+ (floatNum+negativeNum));
   
 }

}

Output: 

float value +ve :: 43.4
float value -ve :: -43.4
adding both :: 0.0

f in the value represents that it is a float literal, otherwise, Java considers it as a double.

Explanation:

In the above code, we assigned a positive and negative float values to two variables and printed them. Lastly, we performed addition operation on both the values and got 0 as the output.

Java float to int

Both float and int are 32-bit primitive types, but float supports value range more than int. Likely, a compiler performs down casting to convert a float to int. It simply ignores or cuts off the fractional part and assigns its non-fractional value to the respective int variable. For example, consider a float literal 1.890,converting it would give 1 as output.

These are the following methods to convert a float type to int.

1. Using Typecasting

As we know, a float can store a larger value than an int type.Therefore, down-casting comes into the picture in order to convert float to int. Below is an example of the same.

Java program for converting float to int

public class FloatExample {
 
   public static void main(String[] args) {
   
     float floatNum = 43.4f;
  
     int intNum = (int) floatNum;

    
    System.out.println("down casting to int :: "+ intNum);
  
  }

}

Output:

down casting to int :: 43

2. Using round method

It is a static method that rounds a number to its nearest integer value. It is an overloaded method, as a result, it can accept float or double, and return an int or long.

Java program for converting float to int using Math round() method

public class FloatExample {
  
  public static void main(String[] args) {
  
     float floatNum = 43.4f;
   
     int intNum = Math.round(floatNum) ;

 
     System.out.println("round off :: "+ intNum);

    }

}

Output:

round off :: 43

Java float to String

The string class provides various methods to convert a float to a string type. Here are the following ways to translate a float to a string representation object.

1. By using String.valueOf() method

A static method accepts float value as an argument and returns its string representation. It has multiple overloaded versions that represents string value for all inbuilt primitive types.

Java program for converting float to string using valueOf() method

public class FloatExample {
   
 public static void main(String[] args) {
 
     float floatNum = 43.4f;
  
     String str = String.valueOf(floatNum) ;

    
     System.out.println("float to string using valueOf :: "+ str);

    }

}

Output:

float to string using valueOf :: 43.4

2. By using toString() method

All wrapper classes support this static method which provides a string representation of an object and Float class is one of them. In this method, one passes float literal as an input parameter and it returns a string object. Below is the program to demonstrate the same.

Java program for converting float to string using toString() method

public class FloatExample {
  
  public static void main(String[] args) {
   
     float floatNum = 43.4f;
     
     String str = Float.toString(floatNum) ;

 
     System.out.println("float to string using toString() :: "+ str);
    
   }

}

Output:

float to string using toString() :: 43.4

3. By using format() method

As the name suggests, it returns a formatted string based on the provided string format and respective object arguments.

Java program for converting float to string using format() method

public class FloatExample {
    
public static void main(String[] args) {
   
     float floatNum = 43.4f;
     
     String str = String.format("%f",floatNum) ;

  
     System.out.println("float to string using format() :: "+ str);
  
  }

}

Output:

float to string using format() :: 43.400002

4. By using string concatenation

It is the simplest way to convert a float to a string object. In this, we concat the desired value with an empty string using + operator to get a string object.

Java program for converting float to string using concatenation operator

public class FloatExample {
  
  public static void main(String[] args) {
  
      float floatNum = 43.4f;
     
      String str = ""+floatNum ;

    
      System.out.println("float to string using format() :: "+ str);
   
  }

}

Output:

float to string using format() :: 43.4

5. By using stringBuilder or stringBuffer class

StringBuilder and StringBuffer class are used to create mutable strings. These classes support various methods and append() is one of them.

Java program for converting float to string using stringBuilder or stringBuffer class

public class FloatExample {
   
 public static void main(String[] args) {
  
   float floatNum = 43.4f;
    
   StringBuffer stringBuffer = new StringBuffer();
 
   StringBuilder stringBuilder = new StringBuilder();
     
   stringBuilder.append(floatNum);
   
   stringBuffer.append(floatNum);

 
   System.out.println("using stringBuffer :: "+ stringBuffer);
 
   System.out.println("using stringBuilder :: "+ stringBuilder);


 }

}

Output:

using stringBuffer :: 43.4
using stringBuilder :: 43.4

Frequently Asked Questions:

1. What is float in Java?

float is a single-precision primitive data type for storing fractional values.

2. What is default value and size of float in Java?

For float, its default value is 0.0f and the size is 32 bits

float is a single-precision primitive data type for storing fractional values.

3. Can one assign a decimal value to float type in Java?

By default, Java treats a decimal value as a double type, and therefore, a compiler throws exception while assigning it to float variable. To make the value as float, we need to append ‘f’ at the end of it i.e 4.56f.

4. How do I limit the number of decimal places in Java?

Java supports a format method that returns formatted string. It can be used for limiting decimal places in Java.

Below is the program :

public class FloatExample {

public static void main(String[] args) {

float floatNum = 10.573425253f;

String str = String.format("%.3f",floatNum); // consider 3 value after decimal point
System.out.println(" Output :: "+ str);


}
}

5. Can Float in Java be negative?

Yes, float is a signed primitive type, meaning, it can hold both positive and negative values.

6. Write a program to create a method that returns float value in Java?

public class FloatExample {

public static void main(String[] args) {

FloatExample floatExample = new FloatExample();

float result = floatExample.getOutput(3.3f, 1.1f);

System.out.println("Result :: " + result);

}


private float getOutput(float a, float b) {

return a / b;

}

}

7. Difference between float and double in Java

1. float is a 32-bit type, whereas, double is 64-bit.

2. float represents single-precision values, whereas, double specifies double precision. It signifies, a double provides more accurate value than float.

3. float’s default value is 0.0f, on the other hand, 0.0d for double.

4. float can be used for saving memory in large arrays, in contrast, double can be used to get a more precise result.

5. float keyword is used to declare float value, whereas, double keyword is used to declare double type.

6. It leads to data loss if a double is converted to float, while, double supports a wider range, therefore, no data loss can be viewed while converting float to double.

Leave a Reply