- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 save decimal
Let’s say the value is 0.10705921712947473 and you want to save only the first 3 decimal digits. At first let us declare the values −
double val = 320.0 / 2989.0; BigDecimal big = new BigDecimal(val);
Now, use setScale() and set the parameter as 3 to save 3 decimal places −
big = big.setScale(3, RoundingMode.HALF_DOWN);
Example
import java.math.BigDecimal; import java.math.RoundingMode; public class Demo { public static void main(String[] args) { double val = 320.0 / 2989.0; BigDecimal big = new BigDecimal(val); System.out.println(String.format("Value = %s", val)); // scale big = big.setScale(3, RoundingMode.HALF_DOWN); double val2 = big.doubleValue(); System.out.println(String.format("Scaled result = %s", val2)); } }
Output
Value = 0.10705921712947473 Scaled result = 0.107
- Related Articles
- Java Program to convert Decimal to Octal
- Java Program to convert from decimal to binary
- Java Program to convert from decimal to hexadecimal
- Java program to convert decimal number to hexadecimal number
- Java program to convert decimal number to binary value
- Java program to convert decimal number to octal value
- Java program to convert binary number to decimal value
- Java program to convert float decimal to Octal number
- Java Program to convert binary number to decimal number
- Java Program to convert decimal integer to octal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert octal number to decimal number
- Java Program to convert decimal integer to hexadecimal number
- Java Program to Round a Number to n Decimal Places
- Java Program to create Big Decimal Values via a string

Advertisements