Home > Java > javaTutorial > FloatValue() method for double precision floating point numbers using example in Java

FloatValue() method for double precision floating point numbers using example in Java

王林
Release: 2023-08-18 18:34:10
forward
969 people have browsed it

FloatValue() method for double precision floating point numbers using example in Java

Java provides many tools and functions when dealing with precision-based calculations. One such feature is handling floating point numbers, specifically using the Double and Float wrapper classes. Today, we will demystify the Double and Float Value() methods in Java.

grammar

In Java, the Value() function in the Double and Float classes is defined as follows:

public double doubleValue()
public float floatValue()
Copy after login

Glossary explanation

These techniques are part of the Twofold and Float overlay classes. The doubleValue() method converts a Twofold object to a double-precision primitive value, while the floatValue() method converts a Float object to a single-precision primitive value. Both methods have no parameters.

algorithm

When you use these methods, the following is the basic algorithm that works:

  • Declare and initialize a Double or Float object with a numerical value.

  • Call the doubleValue() or floatValue() method on the object.

  • This method returns the original double or floating point value of the object.

Method 1: Use Value() to perform mathematical operations

You can use the Value() method to convert a Double or Float object to a primitive data type, allowing you to use them in mathematical operations.

Example

public class Main {
   public static void main(String[] args) {
      Double obj = new Double(20.5);
      double primitive = obj.doubleValue();
      double result = primitive / 2; // mathematical operation
      System.out.println(result); // prints 10.25
   }
}
Copy after login

Output

10.25
Copy after login
The translation of

Explanation

is:

Explanation

This Java program demonstrates the doubleValue() method of the Double wrapper class. It first creates a Double object obj with a value of 20.5. Then call the doubleValue() method on obj, convert it to the original double type, and store it in primitive. Next, a mathematical operation is performed, specifically dividing the primitive by 2, and the result is stored in the result. Then the line System.out.println(result) outputs the result of the division (10.25) to the console. This example highlights the use of the doubleValue() method to convert a Double object to the original double data type, allowing direct mathematical operations to be performed on the value.

Method 2: Use Value() in comparison operations

Value() is also useful when you need to compare floating point numbers.

Example

public class Main {
   public static void main(String[] args) {
      Double obj1 = new Double(20.5);
      Double obj2 = new Double(30.5);
      boolean isEqual = obj1.doubleValue() == obj2.doubleValue(); // comparison operation
      System.out.println(isEqual); // prints false
   }
}
Copy after login

Output

false
Copy after login
The translation of

Explanation

is:

Explanation

This Java program demonstrates an example of using the doubleValue() method to compare Double objects. Initially, two Double objects obj1 and obj2 are created and initialized, assigned values ​​of 20.5 and 30.5 respectively. Use the doubleValue() method to convert these Double objects into original double values. Then use the equal sign (==) to compare these double values. The result of the comparison is stored in the Boolean variable isEqual. Since 20.5 is not equal to 30.5, isEqual is false. The System.out.println(isEqual) statement prints false to the console. This code demonstrates how to compare two Double objects in Java, which is an important task in many programming scenarios.

Method 3 of using Value() in conditional operations: Approach 3

When using conditional statements, Value() is also very convenient.

Example

public class Main {
   public static void main(String[] args) {
      Float obj = new Float(20.5f);
      if (obj.floatValue() > 20.0f) {
         System.out.println("Greater than 20"); // prints "Greater than 20"
      }
   }
}
Copy after login

Output

Greater than 20
Copy after login
The translation of

Explanation

is:

Explanation

This Java program demonstrates the use of the floatValue() method of the Float wrapper class in conditional statements. First, create a Float object obj with a value of 20.5f. Then, use the floatValue() method to convert the Float object to the basic float type. Then, compare this float value with 20.0f and put it in the if statement. If the float value of obj is greater than 20.0f (because 20.5f is greater than 20.0f), the program will print the string "More noteworthy than 20" on the console. This is a common use case for the floatValue() method, allowing Float objects to be used directly in conditional operations, demonstrating the power and flexibility of Java's wrapper classes.

Method 4 of using the Value() method in the object array

The Value() method can also be employed when dealing with an array of Double or Float objects.

Example

public class Main {
   public static void main(String[] args) {
      Double[] objArray = {new Double(10.5), new Double(15.5), new Double(20.5)};
      for (Double obj : objArray) {
         System.out.println(obj.doubleValue()); // prints 10.5, 15.5, 20.5
      }
   }
}
Copy after login

Output

10.5
15.5
20.5
Copy after login
The translation of

Explanation

is:

Explanation

The provided Java program demonstrates the use of the doubleValue() method and Double object array. In this program, an array called objArray is created and filled with three Double objects, holding the values ​​10.5, 15.5, and 20.5 respectively. The program then uses an enhanced for loop to iterate through each Double object in the array. Inside the loop, call the doubleValue() method on the current Double object to convert it to the original double data type. This value is then printed to the console. So when the program runs, 10.5, 15.5, and 20.5 will be printed out in order. This program demonstrates how the doubleValue() method simplifies working with an array of Double objects.

in conclusion

In Java, the Worth() strategy of the Twofold and Float overlay classes are useful resources for handling floating point numbers. They provide a simple, clear way to convert Twofold and Float objects into double- and single-precision native types, making these data types more flexible and easier to use. Whether you are performing mathematical operations, comparing numbers, providing conditional expressions, or manipulating arrays, the Worth() method makes your life as a Java developer much simpler. Happy coding!

The above is the detailed content of FloatValue() method for double precision floating point numbers using example in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template