More Arithmetic Operations
![]() |
Java - Arithmetic Operations |
Introduction
We studied arithmetic operators in the previous units to carry out simple mathematical calculations.
Apart from that, we will study some in-built math methods present in the Math class in order to carry out more mathematical operations and how to include comments in Java.
1. Math Class
Java Math class offers various methods for carrying out mathematical operations like exponentiation, rounding off, etc.
Math is in java.lang package so need not be imported.
Let us see some of its methods.
pow()
round()
1.1 pow()
The pow() method is analogous to ** operator in Python. It takes two numbers and gives the value of the first number raised to the second number (exponentiation). It returns a double type of value.
Syntax
Math.pow(base, exponent);
Here, the base and exponent must be of double type.
Key Points:
If the second parameter is zero then the output will be 1.0.
When we call the pow() method by passing an int value, it automatically calls the double version of the method because an int value is automatically converted to a double value.
For example:
Code :
class Main {
public static void main(String[] args) {
int base = 6;
int exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(result);
}
}
Output:
216.0
1.2 round()
The round() method rounds the specified value to the nearest int or long value.
Syntax
Math.round(num);
Here, num is the number to be rounded (it should be of float or double type)
It returns the int value if argument is float
It returns the long value if the argument is double
Example 1: round() with float value
Code:
class Main {
public static void main(String[] args) {
float floatNum = 10.53f;
int rounded = Math.round(floatNum);
System.out.println(rounded);
}
}
Output:
11
In the above code, since the float type is passed to the round() method, it gave back the int value.
Example 2: round() with double value
Code
class Main {
public static void main(String[] args) {
double doubleNum = 5.654d;
long rounded = Math.round(doubleNum);
System.out.println(rounded);
}
}
Output:
6
In the above code, since the double type was passed to the round() method, it gave back a long value.
2. DecimalFormat Class
The DecimalFormat class of package java.text employed for formatting decimal numbers.
Syntax
DecimalFormat df = new DecimalFormat("#.###");
df.format(num);
Here,
df is the variable name (object of DecimalFormat).
# after the decimal point denotes how many decimal digits we are rounding off to.
num is the number to be rounded off.
First, we have to create a DecimalFormat object and then invoke the format() method of the object to round off the numbers given.
format() method accepts a double or long value and returns a String value
If float or int are data types are passed, they get automatically converted to double and long respectively.
Code:
import java.text.DecimalFormat;
class Main
{
public static void main(String[] args) {
osomal
double doubleNum = 1.666;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(doubleNum));
}
}
Output:
1.67
4. Floating Point Errors
In Java, float values are stored approximately in the same manner as in Python. Sometimes, floating-point approximation gives unexpected results.
let's check it with an example,
Code
class Main {
public static void main(String[] args) {
System.out.println(0.1 + 0.2);
System.out.println((0.1 + 0.2) == 0.3 );
}
}
Output:
0.30000000000000004
false
In the above code, we can observe that 0.1 + 0.2 is calculated as 0.30000000000000004. Therefore, comparing this value with 0.3 returned false.
Summary
The Math class offers numerous methods to execute math operations.
pow(): It computes the exponents and returns the result.
round(): It rounds the given value to the nearest int or long value and returns it.
The java.text package's DecimalFormat class used for formatting decimal numbers.
Related Posts :
Java Introduction & Basics
Sequence of Instructions in Java
Java Data Types
Java Input and Output Basics
Java Type Conversions
Java String Methods
Java Logical Operators
Conditional Statements in Java
Java Ternary Operator
Java Switch Statement
Loops in Java
Java String Method