Found 9150 Articles for Object Oriented Programming

Trigonometric methods in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:04:16

452 Views

The java.lang.Math class contains methods for performing basic numeric operations such as the trigonometry, logarithm, etc.The following are some of the methods.Sr.NoMethods & Description1static double abs(double a)This method returns the absolute value of a double value.2static float abs(float a)This method returns the absolute value of a float value.3static int abs(int a)This method returns the absolute value of an int value.4static long abs(long a)This method returns the absolute value of a long value.5static double acos(double a)This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.6static double asin(double a)This method returns the arc ... Read More

Format double type in Java

Samual Sam
Updated on 26-Jun-2020 09:05:22

3K+ Views

Let’s say we have the following three values −double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));The following is an example where we have also shown other ways to format double in Java.Example Live Demopublic class Demo {    public static void main(String args[]) {       double val1 = 15.546;     ... Read More

Double isNaN() method in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:51:15

184 Views

The java.lang.Double.isNan() method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.Let’s say the following are our Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Now, we will use the isNan() method to check whether the number is a NaN or not.val1.isNaN(); val2.isNaN()The following is our final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isNaN());       System.out.println(val2.isNaN());    } }Outputfalse true

Double isInfinite() method in Java

Samual Sam
Updated on 26-Jun-2020 08:51:51

131 Views

The Double isInfinite() method returns true if this Double value is infinitely large in magnitude, false otherwise.Let’s say we have the following Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Use the isInfinite() method now. The return value is a boolean.val1.isInfinite(); val2.isInfinite();The following is the final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isInfinite());       System.out.println(val2.isInfinite());    } }Outputtrue false

Convert double primitive type to a Double object in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:52:45

8K+ Views

To convert double primitive type to a Double object, you need to use Double constructor.Let’s say the following is our double primitive.// double primitive double val = 23.78;To convert it to a Double object, use Double constructor.// Double object Double ob = new Double(val);Example Live Demopublic class Demo {    public static void main(String args[]) {       // double primitive       double val = 23.78;       // Double object       Double ob = new Double(val);       System.out.println(ob);    } }Output23.78

Format floating point number in Java

Revathi Satya Kondra
Updated on 13-Dec-2024 13:38:06

4K+ Views

What is Floating Point Number? Floating point number formatting is the process of controlling how floating point numbers (numbers with decimal points) are displayed(printed in a program). For example, if we have a decimal number of 123.45678, the formatted number can be 123.457, 123.46, and 123.5 instead of that number. This makes the numbers easier to read and more consistent. The Floating point is just a number with decimal points. For example 1.2, 2.45 and 123.4 are the examples of the point numbers. It helps ensure the number of decimal places, making numerical data easier to read and maintaining a ... Read More

Check two float arrays for equality in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:45:58

179 Views

To check two float arrays for equality, use the Arrays.equals() method.In our example, we have the following two float arrays.float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };Let us now compare them for equality.if (Arrays.equals(floatVal1, floatVal2)) { System.out.println("Both are equal!"); }The following is an example wherein we compare arrays and with that also use == to check for other conditions.Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };       ... Read More

Java Program to compare Two Java float Arrays

Samual Sam
Updated on 26-Jun-2020 08:46:48

200 Views

To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f }; float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f }; float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };To compare them, the Arrays.equals() method is to be used −Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);The following is the complete example wherein we compare all the arrays −Example Live Demoimport java.util.Arrays; public class Demo {    public static void ... Read More

Java Program to convert a String to a float type Number

karthikeya Boyini
Updated on 23-Jan-2025 22:54:22

222 Views

In this article, we will learn how to convert a string to a float-type number in Java. We will use the Float.parseFloat() method to perform this conversion. Float.parseFloat() method The Float.parseFloat() method belongs to the Float class in Java and is used to convert a string representation of a number into a primitive float. If the string does not contain a valid float representation, it throws a NumberFormatException. NumberFormatException: Thrown to show that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. ... Read More

Convert from String to float in Java

Samual Sam
Updated on 26-Jun-2020 08:48:08

1K+ Views

To convert String to float, use the valueOf() method.Let’s say we have the following string value.String str = "0.8";Converting the string to float.Float floatVal = Float.valueOf(str).floatValue();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "0.8";       Float floatVal = Float.valueOf(str).floatValue();       System.out.println("Float: "+floatVal);    } }OutputFloat: 0.8

Advertisements