Home » Java Programs » Java Round Double to 2 Decimal Places

# Java Round Double to 2 Decimal Places

Simple java round double to 2 decimal places. In algebra, a decimal number is defined as a number with a decimal point separating the entire number and fractional parts. A decimal point is the dot in a decimal number.

## Decimal Number Definition in Java

A decimal number is a number in algebra that has a decimal point separating the whole number and fractional parts. A decimal point is the dot at the end of a decimal number. When rounding a decimal number, there are some guidelines to follow.

Simply put, if the last digit is less than 5, the previous digit is rounded down. If it’s 5 or more, however, you should round up the previous digit. So, if you’re about to round a number and it’s followed by 5, 6, 7, 8, 9, round it up.

## .f in Java Program

We should append the letter f to the end of a float data type when representing it in Java; otherwise, it will save as a double. In Java, the default value for a float is 0.0f. When you want to save memory and your calculations don’t require more than 6 or 7 digits of precision, you should use the float data type. Here we have executed the java round double to 2 decimal places for beginners.

## System.out.format in Java Program

The first parameter, format, specifies how the objects in the second parameter, args, should be formatted. The format string contains both plain text and format specifiers, which are special characters that format the  Object. args arguments.

System. out print() and println() The sole difference between println() and print() is that after publishing the required result, println() moves the cursor to the next line, whereas print() retains the cursor on the same line.

The shorthand for “print line” is println, which means that when the input is printed, the program moves on to the next line. printf stands for print formatter, and it allows you to mark where String variables should go and pass those variables in with it.

## Java Round Double to 2 Decimal Places

``````//java round double to 2 decimal places
public class Main
{
public static void main(String[] args)
{
double num = 4.644567;
System.out.format("%.2f", num);
}
}``````

Output:

4.64