
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
Java Program to Save Decimal
In this article, we will learn two ways to handle decimal values in Java. First, we will use the BigDecimal class to ensure precision when working with decimals. Then, we will use the DecimalFormat class to format decimal values. Both examples will show how to scale decimal values to three places.
Different Approaches to Save Decimal Values
The following are the two approaches to save decimal values in Java ?
Save Decimal Values Using BigDecimal for Precision Control
The BigDecimal class of java.math package is used for tasks like arithmetic, scaling, rounding, comparisons, hashing, and converting formats.
- setScale() Method: The setScale(int newScale, RoundingMode roundingMode) method returns a BigDecimal with the specified scale. It adjusts the value by multiplying or dividing it by the right power of ten to keep the value the same.
Creating a BigDecimal from the result ?
BigDecimal big = new BigDecimal(val);
Scaling the value to 3 decimal places using RoundingMode.HALF_DOWN ?
big = big.setScale(3, RoundingMode.HALF_DOWN);
Converting the scaled BigDecimal back to a double ?
double val2 = big.doubleValue();
Example
Below is the Java program to save decimals using the BigDecimal class ?
import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { // Divide two numbers to get a decimal value double val = 320.0 / 2989.0; BigDecimal big = new BigDecimal(val); System.out.println(String.format("Value = %s", val)); // Scale the value to 3 decimal places using RoundingMode.HALF_DOWN 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
Time complexity: O(n) for operations like scaling (where n is the number of digits),
Space complexity: O(n) due to storing the precise representation.
Save Decimal Values Using DecimalFormat
The DecimalFormat class is a part of java.text package and used to format numbers based on a custom pattern, allowing control over decimal places, grouping, and more. It also supports locale-specific formatting, adjusting numbers for different regions.
- The df.format(val) method formats the given number val according to the pattern defined in the DecimalFormat instance df. It returns the formatted number as a string, applying rules like decimal places, grouping, and locale-specific settings.
Creating a DecimalFormat instance with the desired pattern ?
DecimalFormat df = new DecimalFormat("#.###");
Formatting the value into a string ?
String formattedVal = df.format(val);
Example
Below is the Java code to save decimals using DecimalFormat ?
import java.text.DecimalFormat; public class Demo { public static void main(String[] args) { // Divide two numbers to get a decimal value double val = 320.0 / 2989.0; // Create a DecimalFormat instance with the desired pattern DecimalFormat df = new DecimalFormat("#.###"); // Format the value String formattedVal = df.format(val); // Print the original and formatted values System.out.println("Value = " + val); System.out.println("Scaled result = " + formattedVal); } }
Output
Value = 0.10714473051409899
Scaled result = 0.107
Time complexity: O(k) (where k is the number of characters in the formatted string),
Space complexity: O(k) for the formatted output string.