Fractional Part of a Float in Java

Fractional Part of a Float in Java thumbnail
Java programs can handle various number types.

The Java language can model variables of many different numerical types. Developers can store floating point numbers as values or variables. The float wrapper class also provides methods for processing floating point values. When a program contains a float number, the developer can determine the value of the fractional part using a simple arithmetic calculation. This calculation involves determining the remainder of the value following a division operation.

  1. Float Variable

    • Java programs can model floating point values as primitive type variables. The following sample code demonstrates creating and instantiating a float variable with a value:

      float myNumber = 5.7f;

      This variable stores the specified floating point value. The program can carry out arithmetic calculations and other numerical processing using this variable. The variable can have any name of the programmer's choice, but it is generally recommended to choose names that are meaningful within the application context.

    Modulus

    • The remainder operator in Java allows code to determine the remaining value following division by a particular number. To determine the fractional part of a value such as a float or double, programs can use the remainder after dividing by one, as follows:

      float fractionalPart = myNumber%1;

      This new float variable contains the fractional part of the original variable. The result is also a floating point type number, so the developer can carry out further numeric processing on it if necessary.

    Float Object

    • Some Java programs model floating point number values using the float wrapper class. The following sample code demonstrates creating a float object using the original primitive type float variable as a parameter:

      Float numObject = Float.valueOf(myNumber);

      If a program has a floating point number modeled in this way, it must convert it back to a primitive type to carry out the remainder operation, as follows:

      float objectFraction = numObject.floatValue()%1;

      This calculates the fractional part as a remainder then stores the result in a new primitive type float variable.

    Conversion

    • Programs in which floating point numbers are represented often need to convert values and variables between types. Using casting, developers can convert number values between primitive types, including floating point variables, as follows:

      double newNum = (double)myNumber;

      This converts the float value to a double, storing the result in a new primitive type variable. This is common if a program needs a precise value for the fractional part. The code can access the fractional remainder of the double value using the same technique as for the float, as follows:

      double fractionDouble = newNum%1;

      This contains the double version of the fractional part for the original float.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured