- 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
Truncate BigDecimal value in Java
The truncate BigDecimal value in Java, try the following code.
Here, we have taken two BigDecimla values and set the round mode for truncating the decimal values −
Example
import java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { int decPlaces1 = 3; int decPlaces2 = 5; BigDecimal val1 = new BigDecimal("37578975587.876876989"); BigDecimal val2 = new BigDecimal("62567875598.976876569"); System.out.println("Value 1 : "+val1); System.out.println("Value 2 : "+val2); // ROUND_DOWN val1 = val1.setScale(decPlaces1, BigDecimal.ROUND_DOWN); String str1 = val1.toString(); System.out.println("Result = "+str1); // ROUND_UP val2 = val2.setScale(decPlaces2, BigDecimal.ROUND_UP); String str2 = val2.toString(); System.out.println("Result = "+str2); } }
Output
Value 1 : 37578975587.876876989 Value 2 : 62567875598.976876569 Result = 37578975587.876 Result = 62567875598.97688
- Related Articles
- Negate a BigDecimal value in Java
- Multiply one BigDecimal to another BigDecimal in Java
- Subtract one BigDecimal from another BigDecimal in Java
- Java Program to divide one BigDecimal from another BigDecimal
- Java Program to create a BigDecimal from a string type value
- Math operations for BigDecimal in Java
- Working with BigDecimal values in Java
- Create a BigDecimal via string in Java
- Compare BigDecimal movePointRight and scaleByPowerOfTen in Java
- Create BigDecimal Values via a long in Java
- How to truncate a file in Java?
- Truncate results in decimal to integer value with MySQL
- Java Program to round a double passed to BigDecimal
- How to use BigDecimal in Ruby?
- C# Truncate Method

Advertisements