Java Program - Convert Int to Float - Tutorial Kart
[Pages:4]Java Program to Convert Int to Float
Java ? Convert Int to Float
In this tutorial, we shall learn how to convert an integer value to a float value.
Firstly, we shall go through the process of widening casting, where we shall take the advantage of implicit casting of lower datatype to higher datatypes.
Secondly, we shall discuss about Float.intBitsToFloat() method, which converts the representation of integer bits to float bits.
Also, there is a third method of using Integer.floatValue(). This is kind of a wrapper to the first process. But anyway, we shall go through an example using Integer.floatValue() method.
Convert Int to Float ? Widening Primitive Conversion
Int is a smaller datatype and float is a larger datatype. So, when you assign an int value to float variable, the conversion of int to float automatically happens in Java. This is also called widening casting or widening primitive conversion.
So, to convert an in to float using widening casting, you can just assign a value of int to float.
In the following example, we have a variable i which is of int type. We shall assign i to float variable f during its declaration.
Java Program
/** * Java Program - Convert Int to Float */ public class IntToFloat {
public static void main(String[] args) { int i = 125; float f = i; System.out.println(f);
} }
Output
125.0
Convert Int Bits to Float ? Float.intBitsToFloat()
Integer has a size of 4 bytes and float has a size of 4 bytes. Java allows you to convert this integer representation of 4 bytes into float representation using static method intBitsToFloat() of Float class.
In the following example, we shall take an integer variable initialized to a hexadecimal representation of 4 bytes. Pass this integer variable as argument to intBitsToFloat() method.
Java Program
/** * Java Program - Convert Int to Float */ public class IntToFloat {
public static void main(String[] args) { int i = 0x40000085; float f = Float.intBitsToFloat(i); System.out.println(f);
} }
Output
2.0000317
Convert Int Bits to Float ? Integer.floatValue()
Integer.floatValue() performs a widening primitive conversion on this integer object and returns a float value.
In the following example, we shall take an integer object initialized to a value, and call floatValue() method on this object.
Java Program
/** * Java Program - Convert Int to Float */ public class IntToFloat {
public class IntToFloat { public static void main(String[] args) { Integer i = 85; float f = i.floatValue(); System.out.println(f); }
}
Output
85.0
Conclusion
In this Java Tutorial, we learned how to write Java programs to convert an Int to Float, with the help of example Java programs.
Java Tutorial Java - String to Int Java - String to Float Java - String to Double Java - String to Long Java - String to Boolean Java - Int to String Java - Int to Float Java - Int to Double Java - Int to Long Java - Int to Char Java - Float to String Java - Float to Int Java - Float to Double Java - Float to Long Java - Long to String Java - Long to Float Java - Long to Double
Java - Long to Int Java - Double to String Java - Double to Float Java - Double to Int Java - Double to Long Java - Char to Int Java - Boolean to String
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java program convert int to char tutorial kart
- php how to convert string to int tutorial kart
- convert string variables to numeric variables and vice versa stata
- datetime conversion — converting strings to stata dates
- text to vb string crack keygen download latest 2022
- convert string to int sql server example
- collections visual basic
- java program convert int to float tutorial kart
- declare string array vb
- statistical software for data science stata
Related searches
- convert string to float tableau
- java program convert binary to decimal
- convert int to int array
- java convert int to integer
- convert int to float java
- convert array to float python
- convert string to float in python
- how to convert int to string
- how to convert int to char
- convert string to float python
- how to convert int to int
- how to convert int to hex python