Even though both float as well as int are 32-bit broad information type, float has the higher range than integer primitive value. Since a float is a bigger than int, you lot tin convert a float to an int yesteryear simply down-casting it e.g. (int) 4.0f will give you lot integer 4. By the way, you lot must retrieve that type casting precisely acquire rid of anything later on the decimal point, they don't perform whatever rounding or flooring performance on the value. So if your float value is 3.999, downwards casting to an integer will make 3, non 4. If you lot postulate rounding as well as then take in using Math.round() method, which converts float to its nearest integer yesteryear adding +0.5 as well as and then truncating it. Math.random() is overloaded for both float as well as double, hence you lot tin purpose this for converting double to long every bit well. Let's meet an instance of converting a float value to int inward Java.
You tin meet that for both 3.14 as well as 3.99 you lot got precisely 3 because type casting doesn't make whatever rounding earlier truncating value later on the decimal point.
Remember, Math.round() is overloaded to convey both float as well as double, the version which accepts float supply int as well as other ane supply long. Since floating betoken seat out is yesteryear default double, you lot brand certain to seat suffix "f" amongst values, otherwise, you lot volition terminate upward calling Math.round(double) method, which require farther casting from long to int every bit shown below:
Here is a summary of all three ways to convert a float to int value inward Java:
Important points to remember
Here are some of import points to Federal Reserve notation spell converting float to int variable inward Java:
1) Even though both int and float are the 32-bit information type, but the float has a higher arrive at than int.
2) You tin downcast float to int to acquire rid of decimal values e.g. casting 3.2 volition give you lot 3.
3) Typecasting doesn't perform whatever rounding, hence casting 9.999 volition give you lot nine as well as non 10.
4) If you lot desire to circular earlier conversion as well as then use Math.round(float), it volition give you lot the nearest integer.
4) Remember, Math.round() is overloaded, ane convey the double as well as some other float. Since floating betoken seat out is yesteryear default double, brand certain you lot put "f" suffix spell passing constant values to Math.round() method.
That's all about how to convert float to int inward Java. Even though you lot tin but convert a float primitive value to int primitive yesteryear downcasting, but you lot must retrieve that type casting doesn't make rounding. It precisely throws away anything later on the decimal point. If you lot desire to circular the float to nearest integer as well as then delight use Math.round() method, which accepts a float as well as returns the nearest integer. Since this method is overloaded, brand certain you lot suffix "f" to your values to ensure that they are considered float as well as double, otherwise the compiler volition telephone band Math.round(double) method, which volition circular but supply long, as well as you lot postulate to farther cast into int to avoid the compiler error.
Related Tutorials
If you lot similar this tutorial as well as wants to larn to a greater extent than close how to convert ane information type into another, as well as then you lot tin besides banking concern gibe out next Java programming tutorials:
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
float to int using type casting
If your requirement is to convert a floating betoken seat out to an integer yesteryear precisely getting rid of those fractional values later on the decimal betoken as well as then down-casting is the best agency to make it. It's simple, efficient as well as elegant, Here is an instance of casting float to int inward Java:int value = (int) 3.14f; // 3 int grade = (int) 3.99f; // 3
You tin meet that for both 3.14 as well as 3.99 you lot got precisely 3 because type casting doesn't make whatever rounding earlier truncating value later on the decimal point.
float to int using Math.round()
This is the correct agency to convert float to int if you lot are interested inward rounding earlier conversion. Math.round() converts a floating-point seat out to the nearest integer yesteryear kickoff adding 0.5 as well as and then truncating value later on the decimal point.int a = Math.round(3.14f); // 3 int b = Math.round(3.99f); // 4 int c = Math.round(3.5f); // 4
Remember, Math.round() is overloaded to convey both float as well as double, the version which accepts float supply int as well as other ane supply long. Since floating betoken seat out is yesteryear default double, you lot brand certain to seat suffix "f" amongst values, otherwise, you lot volition terminate upward calling Math.round(double) method, which require farther casting from long to int every bit shown below:
// casting required because Math.round(double) returns long int a = (int) Math.round(3.14);
Here is a summary of all three ways to convert a float to int value inward Java:
Java Program to convert float to int
Let's meet the a complete, running Java programme to examine whatever you lot receive got learned hence far. In the following example, I receive got combined both casting as well as rounding to convert float to an integer inward Java. This convert some floating betoken numbers to int. You tin farther heighten this programme to convey a floating-point seat out from the user as well as and then convert it to an integer. If you lot are novel as well as don't know how to read user input as well as then banking concern gibe out this tutorial./** * Java Program to convert a float value to integer. * * @author WINDOWS 8 * */ public class floatToInteger{ public static void main(String args[]) { // converting a float to int yesteryear casting System.out.println("converting float to int using downcasting"); int value = (int) 3.14f; // 3 int grade = (int) 3.99f; // 3 System.out.printf("float : %f, int : %d %n", 3.14f, value); System.out.printf("float : %f, int : %d %n", 3.99f, score); // converting float to integer value yesteryear rounding System.out.println("converting float to int using rounding"); int a = Math.round(3.14f); // 3 int b = Math.round(3.99f); // 4 int c = Math.round(3.5f); // 4 System.out.printf("float : %f, int : %d %n", 3.14f, a); System.out.printf("float : %f, int : %d %n", 3.99f, b); System.out.printf("float : %f, int : %d %n", 3.5f, c); } } Output: converting float to int using downcasting float : 3.140000, int : 3 float : 3.990000, int : 3 converting float to int using rounding float : 3.140000, int : 3 float : 3.990000, int : 4 float : 3.500000, int : 4
Important points to remember
Here are some of import points to Federal Reserve notation spell converting float to int variable inward Java:
1) Even though both int and float are the 32-bit information type, but the float has a higher arrive at than int.
2) You tin downcast float to int to acquire rid of decimal values e.g. casting 3.2 volition give you lot 3.
3) Typecasting doesn't perform whatever rounding, hence casting 9.999 volition give you lot nine as well as non 10.
4) If you lot desire to circular earlier conversion as well as then use Math.round(float), it volition give you lot the nearest integer.
4) Remember, Math.round() is overloaded, ane convey the double as well as some other float. Since floating betoken seat out is yesteryear default double, brand certain you lot put "f" suffix spell passing constant values to Math.round() method.
That's all about how to convert float to int inward Java. Even though you lot tin but convert a float primitive value to int primitive yesteryear downcasting, but you lot must retrieve that type casting doesn't make rounding. It precisely throws away anything later on the decimal point. If you lot desire to circular the float to nearest integer as well as then delight use Math.round() method, which accepts a float as well as returns the nearest integer. Since this method is overloaded, brand certain you lot suffix "f" to your values to ensure that they are considered float as well as double, otherwise the compiler volition telephone band Math.round(double) method, which volition circular but supply long, as well as you lot postulate to farther cast into int to avoid the compiler error.
Related Tutorials
If you lot similar this tutorial as well as wants to larn to a greater extent than close how to convert ane information type into another, as well as then you lot tin besides banking concern gibe out next Java programming tutorials:
- How to convert String to Double inward Java? (example)
- How to convert from Integer to String inward Java? (tutorial)
- How to convert ByteBuffer to String inward Java? (program)
- How to convert Enum to String inward Java? (example)
- How to convert float to String inward Java? (example)
- How to convert String to Date inward a thread-safe manner? (example)
- How to convert String to int inward Java? (example)
- How to convert byte array to String inward Java? (program)
- How to convert double to Long inward Java? (program)
- How to convert a List to array inward Java? (tutorial)
- How to convert Decimal to Binary inward Java? (example)
- How to convert char to String inward Java? (tutorial)
- How to parse String to long inward Java? (answer)
- How to convert java.sql.Date to java.util.Date inward Java? (article)
- How to convert an array to List as well as Set inward Java? (answer)
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
No comments:
Post a Comment