
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create BigDecimal from String Value in Java
In this article, we will learn to create a BigDecimal from a string value in Java. The BigDecimal class in Java is highly versatile and often used for calculations requiring high precision, such as financial transactions or scientific computations. we'll explore two approaches to creating a BigDecimal from a string value, complete with examples.
When to Use BigDecimal
We can use the BigDecimal in the following conditions ?
- When working with financial calculations requiring precision (e.g., currency operations).
- When handling scientific computations involving large or highly precise numbers.
- When exact results are critical, rounding errors are unacceptable.
Using the BigDecimal Constructor
The simplest way to create a BigDecimal of BigDecimal class is by using its constructor and passing a string value as the parameter. This ensures that the value is represented accurately without any loss of precision.
Following are the steps to create a BigDecimal by using the constructor ?
- The BigDecimal object one is created using a string value "562537627.8787867".
- A second BigDecimal, two, is created using the BigDecimal.valueOf method for demonstration purposes.
- The two BigDecimal objects are added using the add method.
- The result is printed to the console, showcasing the high precision of BigDecimal.
Let us first create a BigDecimal type with string value ?
BigDecimal one = new BigDecimal("562537627.8787867");
Now, create a BigDecimal with long ?
BigDecimal two = BigDecimal.valueOf(562L);
After that use add() with the aug end value i.e. the value in "two" objects ?
one = one.add(two);
Example
Below is an example to create a BigDecimal by using the constructor ?import java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { BigDecimal one = new BigDecimal("562537627.8787867"); BigDecimal two = BigDecimal.valueOf(562L); one = one.add(two); System.out.println(one); } }
Output
562538189.8787867
Using BigDecimal.valueOf
Another approach is using the BigDecimal.valueOf method. While this method is primarily used for converting primitive types like double or long to BigDecimal, it can be useful in certain scenarios where you're dealing with numeric values that do not require the use of a string constructor.
- The BigDecimal.valueOf method is used to create BigDecimal objects from a double or long value.
Example
Below is an example of creating a BigDecimal by using the Valueof method?
import java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { // Creating a BigDecimal from a double value BigDecimal one = BigDecimal.valueOf(562537627.8787867); // Adding another BigDecimal from a long value BigDecimal two = BigDecimal.valueOf(562L); // Performing addition one = one.add(two); // Displaying the result System.out.println(one); } }
Output
562538189.8787867
Comparison Table
Aspect | BigDecimal Constructor | BigDecimal.valueOf |
Input Type | Accepts a String as input. | Accepts double or long as input. |
Precision | Maintains exact precision, as the input is treated as a string. | May lose precision when used with double due to floating-point representation. |
Use Case | Ideal for high-precision calculations and large values. | Useful for straightforward conversions of primitive numeric values. |
Recommended For | Applications needing high precision, such as financial or scientific computations. | General-purpose use when precision loss is not a concern. |