How To Convert Float To Long Or Int Inward Java?

Yesterday ane of the junior from my squad came to me yell for almost how produce y'all I convert a float variable into long or int? He was storing or thence values coming from or thence other organization inwards the database together with alone wanted to shop value earlier the decimal indicate e.g. he was getting "3.144" together with he desire to convert it to "3" to shop into database. Good affair was that API was returning a float primitive value together with y'all don't demand to convert a String to float etc. There are 3 ways to convert a float value into long or int inwards Java, but nosotros volition alone focus on long part. You tin write the Java plan past times next tips to convert float to an int past times replacing long method to their int counterpart. The starting fourth dimension means is to auto-box float primitive into Float object together with telephone phone the longVale() method together with or thence other means is only cast a float to long or int to acquire rid of decimal points.

Alternative y'all tin work Math.round() together with and then cast dorsum to long. No doubt, the minute approach is the easiest if the requirement is only getting rid of anything after decimal indicate but if y'all demand to rounding together with then the 3rd approach is the correct means to go.

In this tutorial, nosotros volition larn all iii ways to perform float to long conversion inwards Java. Btw, if y'all convey exactly started learning Java together with then y'all tin likewise read Core Java for the Impatient By Cay S. Horstmann, ane of the amend books to larn Java, which covers concept similar this inwards adept detail.

 Yesterday ane of the junior from my squad came to me yell for almost how produce y'all I convert a f How to convert float to long or int inwards Java?



3 ways to convert float to long inwards Java

Let's run into unlike approaches to converting a float value to long or int inwards Java together with empathise pros together with cons of each approach. Java supports type casting together with that should last the measure means for information type conversion.



Solution 1 - Casting

As y'all know that Java back upwards type casting together with it's likewise the measure means to convert ane information type into another, but y'all tin alone typecast higher values into  lower. Since float has higher make than long, y'all tin cast it to long using type casting, every bit shown below :

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

It volition non produce anything exceptional but exactly discard anything after the decimal point, thence y'all volition convey value 3 inwards the fromFloat variable.  If y'all desire to convert float to int together with then instead of casting to long y'all should cast float into an int. This is the easiest means to produce this conversion.


Solution two - Float.longValue

Another means to convert a float to long is past times starting fourth dimension auto-boxing float value into Float wrapper object together with and then calling Float.longValue() method. This method internally cast float value into long every bit shown inwards our starting fourth dimension instance together with below code :

public long longValue() {    return (long)value; }

together with hither is how y'all tin work this method for conversion :

Float PIE = 3.14f; long fromFloat = PIE.longValue();

This method is to a greater extent than suitable when y'all convey a Float object rather than float primitive value.


Solution 3 - Math.round together with Casting

Sometimes conversion is non that straightforward every bit discarding anything after the decimal point. You mightiness demand to produce the rounding starting fourth dimension together with conversion later. If that's the instance together with then y'all tin work Math.round() method for rounding starting fourth dimension together with type casting later on to convert  double to long, because Math.round() method returns a double value. Here is sample code to produce this conversion :

// Using Math.round() together with cast dorsum to long float points = 333.322f; long rounded = Math.round(points);     System.out.printf("float : %f, long : %d %n", points, rounded);          points = 333.922f; rounded = Math.round(points);        System.out.printf("float : %f, long : %d %n", points, rounded);  Output : float : 333.321991, long : 333  float : 333.921997, long : 334 

You tin run into the upshot of rounding, inwards starting fourth dimension instance float was rounded-down together with that's why converted long value is 333 piece inwards the minute instance it was rounded upwards together with that's why the long value is 334.

Here is dainty summary of all iii approaches :



Java Program to convert float to long value

Here is the Java plan which combines all iii ways to convert a float to long inwards Java. You tin run into how they operate together with larn the technique to perform or thence other similar conversion e.g. float to int, short, or byte or double to long, int, brusk together with byte.

/**   * Java Program to convert float to long inwards Java   *    * @author    */  public class FloatToLongConverter{      public static void main(String[] args) {          // Autobox float into Float together with and then telephone phone Float.longValue();                  Float PIE = 3.14f;         long fromFloat = PIE.longValue();                  System.out.printf("float value %f, long value %d %n", PIE, fromFloat);          // Simple cast to acquire rid of decimals         float set out = 444.33f;         long aValue = (long) number;         System.out.printf("float value %f, after casting into long %d %n", number, aValue);          // Using Math.round() together with cast dorsum to long         float points = 333.322f;         long rounded = Math.round(points);                  System.out.printf("float : %f, long : %d %n", points, rounded);       } }  Output : float value 3.140000, long value 3  float value 444.329987, after casting into long 444  float : 333.321991, long : 333  


That's all almost how to convert float to a long information type inwards Java. Type casting is the simplest together with correct means to produce if your finish is exactly to acquire rid of decimal values. If y'all desire to circular earlier getting rid of decimal points together with then y'all tin work Math.round() method. BTW, that's non the alone means to circular numbers inwards Java, but it industrial plant for most purposes.

If y'all are novel to Java together with puzzled on who to convert ane affair to or thence other e.g. String to integer, or String to appointment together with then y'all should banking corporation fit out or thence of the information type conversion tutorials from this weblog :

Further Learning
Complete Java Masterclass
answer)
  • How to convert a listing to Stream inwards Java 8? (example)
  • How produce y'all convert Java Object to XML using JAXB? (example)
  • How to convert Fahrenheit to Celsius inwards Java? (program)
  • String to Date conversion inwards Java using SimpleDateFormat degree (solution)
  • 5 ways to convert InputStream to String inwards Java? (solution)
  • How produce y'all format Date to String inwards Java? (answer)
  • XMLGregorianCalendar to Date inwards Java together with Vice-versa (solution)
  • How produce y'all convert Double to String inwards Java? (solution)
  • How produce y'all convert Binary to Decimal inwards Java? (answer)

  • Hope this helps.


    Sumber https://javarevisited.blogspot.com/

    0 Response to "How To Convert Float To Long Or Int Inward Java?"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel