- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to round a double passed to BigDecimal
The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.
Firstly, let us pass a double to BigDecimal −
BigDecimal val = new BigDecimal(9.19456);
Now, we will round it −
val = val.setScale(2, BigDecimal.ROUND_HALF_EVEN);
Above, we have used the field ROUND_HALF_EVEN. It is a rounding mode to round towards the "nearest neighbor" unless both neighbours are equidistant, in which case, round towards the even neighbours
The following is an example −
Example
import java.math.BigDecimal; public class Demo { public static void main(String args[]) { BigDecimal val = new BigDecimal(9.19456); val = val.setScale(2, BigDecimal.ROUND_HALF_EVEN); System.out.println(val); } }
Output
9.19
- Related Articles
- Java Program to divide one BigDecimal from another BigDecimal
- Java program to round a number
- Multiply one BigDecimal to another BigDecimal in Java
- Java Program to create a BigDecimal from a string type value
- Round float and double numbers in Java
- Java Program to Round a Number to n Decimal Places
- Java Program to convert String to Double
- Java Program to round number to fewer decimals
- Subtract one BigDecimal from another BigDecimal in Java
- Negate a BigDecimal value in Java
- Java Program to convert Double to numeric primitive data types
- Java Program to convert Double into String using toString() method of Double class
- Java Program to display double and single quote in a string
- Create a BigDecimal via string in Java
- Java Program to implement Binary Search on double array

Advertisements