How To Convert Float To Long Or Int Information Type Inwards Java?

Yesterday 1 of the novel joiner Java Trainee Engineer from our squad came to me in addition to asked virtually how create you lot I convert a float variable into long or int information type? He was storing about values coming from about other arrangement inward the database in addition to alone wanted to shop value earlier the decimal signal e.g. he was getting "3.144" in addition to he wants to convert it to "3" to shop into database. The practiced affair was that API was returning a float primitive value in addition to you lot don't demand to convert a String to float etc. I asked him whether he needs routing or not, which he wasn't certain but it turns out that he didn't demand that. I explained to him how to create that in addition to that's what you lot volition notice inward this article equally well. In short, in that place are 3 ways to convert a float value into long or int inward Java, but nosotros volition alone focus on long information type part.

The commencement agency to convert a float information type into a long value is to auto-box float primitive into Float object in addition to calls the longValue() method. This is a to a greater extent than structured agency equally other ways are only to cast a float to long or int to acquire rid of decimal points.

You tin likewise write the Java plan yesteryear next these tips to convert a float value to an int by replacing long method to their int counterpart.

Alternative you lot tin run Math.round() in addition to thus cast dorsum to long information type.

There is no doubt, the 2nd approach is the easiest if the requirement is only getting rid of anything later on decimal signal but if you lot demand rounding thus the 3rd approach is the correct agency to convert a float information type to long inward Java.

In this tutorial, you lot volition larn all iii ways to perform float to long conversion inward Java. Btw, If you lot direct maintain precisely started learning Java or desire to fill upward gaps inward your cognition virtually Java programming thus I advise you lot bring together a comprehensive course of report like The Complete Java Masterclass on Udemy. It's non costly, you lot tin acquire it inward the toll of a java in addition to larn Java better.

 Yesterday 1 of the novel joiner Java Trainee Engineer from our squad came to me in addition to asked  How to convert float to long or int information type inward Java?



3 ways to convert a float to long inward Java

Let's come across unlike approaches to converting a float value to long or int inward Java in addition to empathise the pros in addition to cons of each approach. Java supports type casting in addition to that should endure the criterion agency for information type conversion.

Solution 1 - Casting

As you lot know that Java back upward type casting in addition to it's likewise the criterion agency to convert 1 information type into another, but you lot tin alone typecast higher values into lower. Since float has a higher make than long, you lot tin cast it to long using type casting, equally shown below :

float lay out = 444.33f; long aValue = (long) number; // 444

It volition non create anything particular but precisely discard anything later on the decimal signal thus you lot volition direct maintain value 3 inward the fromFloat variable.

If you lot desire to convert float to int thus instead of casting to long you lot should cast float into an int. This is the easiest agency to create this conversion. If you lot desire to larn to a greater extent than virtually type casting inward Java thus I likewise advise you lot accept a await at answer)
  • How to convert a listing to Stream inward Java 8? (example)
  • How create you lot convert Java Object to XML using JAXB? (example)
  • How to convert Fahrenheit to Celsius inward Java? (program)
  • String to Date conversion inward Java using SimpleDateFormat degree (solution)
  • 5 ways to convert InputStream to String inward Java? (solution)
  • How create you lot format Date to String inward Java? (answer)
  • XMLGregorianCalendar to Date inward Java in addition to Vice-versa (solution)
  • How create you lot convert Double to String inward Java? (solution)
  • How create you lot convert Binary to Decimal inward Java? (answer)

  • Thanks for reading this article thus far. If you lot similar this article thus delight portion alongside your friends in addition to colleagues. If you lot direct maintain whatever questions or feedback thus delight driblet a note.  If you lot like, you lot tin likewise follow on Twitter, I alone tweet virtually Java, programming in addition to technical stuff.


    Belum ada Komentar untuk "How To Convert Float To Long Or Int Information Type Inwards Java?"

    Posting Komentar

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel