
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Round a Number to n Decimal Places
In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the CEIL or FLOOR functions.
Below is a demonstration of the same −
Input
Suppose our input is −
Input : 3.1415
Output
The desired output would be −
Output : 3.2
Algorithm
Step 1 - START Step 2 - Declare a float variable values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 – Use the CEIL function to round the number to the required decimal places. In this example we are rounding up to 2 decimal places. Store the result. Step 5- Display the result Step 6- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class DecimalFormatting { public static void main(String[] args) { float my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.print("Enter the first binary number : "); my_input = my_scanner.nextFloat(); DecimalFormat roundup_decimal = new DecimalFormat("#.#"); roundup_decimal.setRoundingMode(RoundingMode.CEILING); System.out.println("The rounded up value of " +my_input + " is "); System.out.println(roundup_decimal.format(my_input)); } }
Output
Required packages have been imported A scanner object has been defined Enter the first binary number : 3.1415 The decimal number is defined as 3.1415 The rounded up value of 3.1415 is 3.2
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalFormatting { public static void main(String[] args) { System.out.println("Required packages have been imported"); double my_input = 3.1415; System.out.println("The decimal number is defined as " +my_input); DecimalFormat roundup_decimal = new DecimalFormat("#.#"); roundup_decimal.setRoundingMode(RoundingMode.CEILING); System.out.println("The rounded up value of " +my_input + " is "); System.out.println(roundup_decimal.format(my_input)); } }
Output
Required packages have been imported The decimal number is defined as 3.1415 The rounded up value of 3.1415 is 3.2
- Related Articles
- Swift Program to Round a Number to n Decimal Places
- Haskell Program to Round a Number to n Decimal Places
- Kotlin Program to Round a Number to n Decimal Places
- C++ Program to Round a Number to n Decimal Places
- How to round a number to n decimal places in Java
- I want to round a number to 2 decimal places in SAPUI5. Could anyone help?
- Java program to round a number
- C++ Program to compute division upto n decimal places
- Trying to round a calculation to two decimal places in a new column with MySQL?
- Java Program to round number to fewer decimals
- Java Program to format to 2 decimal places in a 10-character field
- Python - Round number of places after the decimal for column values in a Pandas DataFrame
- Java program to convert decimal number to hexadecimal number
- Java Program to convert binary number to decimal number
- Java Program to convert hexadecimal number to decimal number

Advertisements