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 past times simply down-casting it e.g. (int) 4.0f will give you lot integer 4. By the way, you lot must yell back that type casting precisely acquire rid of anything subsequently the decimal point, they don't perform whatever rounding or flooring functioning on the value. So if your float value is 3.999, downward casting to an integer will make 3, non 4. If you lot withdraw rounding as well as then visit using Math.round() method, which converts float to its nearest integer past times adding +0.5 as well as and then truncating it. Math.random() is overloaded for both float as well as double, thus you lot tin role this for converting double to long equally well. Let's run into an instance of converting a float value to int inward Java.
You tin run into 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 subsequently the decimal point.
Remember, Math.round() is overloaded to bring both float as well as double, the version which accepts float supply int as well as other i supply long. Since floating betoken release is past times default double, you lot brand certain to position suffix "f" alongside values, otherwise, you lot volition halt upwardly calling Math.round(double) method, which require farther casting from long to int equally 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 piece 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 hit 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, thus casting 9.999 volition give you lot ix 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, i bring the double as well as some other float. Since floating betoken release is past times default double, brand certain you lot put "f" suffix piece passing constant values to Math.round() method.
That's all about how to convert float to int inward Java. Even though you lot tin precisely convert a float primitive value to int primitive past times downcasting, but you lot must yell back that type casting doesn't make rounding. It precisely throws away anything subsequently 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 phone Math.round(double) method, which volition circular but supply long, as well as you lot withdraw to farther cast into int to avoid the compiler error.
Related Tutorials
If you lot similar this tutorial as well as wants to acquire to a greater extent than well-nigh how to convert i information type into another, as well as then you lot tin too depository fiscal establishment fit out next Java programming tutorials:
Further Learning
If you lot are a beginner inward Java as well as then I advise to start reading Head First Java 2d Edition, it volition boost your learning. If you lot know Java piffling chip as well as wants to buy the farm a professional person Java developer the I advise you lot read Core Java Volume 2 past times Cay S. Horstmann. This mass volition assistance you lot to acquire some advanced characteristic of Java e.g. JDBC, XML processing, Email etc.
float to int using type casting
If your requirement is to convert a floating betoken release to an integer past times precisely getting rid of those fractional values subsequently 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 run into 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 subsequently 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 release to the nearest integer past times start adding 0.5 as well as and then truncating value subsequently 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 bring both float as well as double, the version which accepts float supply int as well as other i supply long. Since floating betoken release is past times default double, you lot brand certain to position suffix "f" alongside values, otherwise, you lot volition halt upwardly calling Math.round(double) method, which require farther casting from long to int equally 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 run into the a complete, running Java programme to attempt whatever you lot receive got learned thus 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 bring a floating-point release 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 depository fiscal establishment fit 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 past times 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 past times 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 piece 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 hit 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, thus casting 9.999 volition give you lot ix 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, i bring the double as well as some other float. Since floating betoken release is past times default double, brand certain you lot put "f" suffix piece passing constant values to Math.round() method.
That's all about how to convert float to int inward Java. Even though you lot tin precisely convert a float primitive value to int primitive past times downcasting, but you lot must yell back that type casting doesn't make rounding. It precisely throws away anything subsequently 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 phone Math.round(double) method, which volition circular but supply long, as well as you lot withdraw to farther cast into int to avoid the compiler error.
Related Tutorials
If you lot similar this tutorial as well as wants to acquire to a greater extent than well-nigh how to convert i information type into another, as well as then you lot tin too depository fiscal establishment fit 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
If you lot are a beginner inward Java as well as then I advise to start reading Head First Java 2d Edition, it volition boost your learning. If you lot know Java piffling chip as well as wants to buy the farm a professional person Java developer the I advise you lot read Core Java Volume 2 past times Cay S. Horstmann. This mass volition assistance you lot to acquire some advanced characteristic of Java e.g. JDBC, XML processing, Email etc.
0 Response to "How to convert float to int inward Java? Examples"
Posting Komentar