I used DecimalFormat for formatting the BigDecimal instead of formatting the String, seems no problems with it.

The code is something like this:

bd = bd.setScale(2, BigDecimal.ROUND_DOWN);

DecimalFormat df = new DecimalFormat();
            
df.setMaximumFractionDigits(2);
            
df.setMinimumFractionDigits(0);
            
df.setGroupingUsed(false);

String result = df.format(bd);
Answer from res1 on Stack Overflow
🌐
Coderanch
coderanch.com › t › 559104 › java › Big-Decimal-String
Big Decimal to String (Beginning Java forum at Coderanch)
November 17, 2011 - Is this present in 1.4? http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) ... Or create the BigDecimal from a String: Not only will this maintain the trailing zeros (also in toString()), but it will also ensure that the exact value is stored.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-tostring-method-in-java-with-examples
BigDecimal toString() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - Next, an adjusted exponent is calculated which is one less than adding the number of characters in the converted unscaled value and negated scale value. That is, -scale + (ulength-1), where ulength is the length of the absolute value of the ...
🌐
Java2Blog
java2blog.com › home › conversion › java bigdecimal to string
Java convert BigDecimal to String
January 12, 2021 - You can also use String’s ValueOf() method to convert BigDecimal to String but it internally uses BigDecimal’s toString method only, so it is better to use BigDecimal’s toString method · When you run above program, you will get below output: ... You might not find expected result when you use BigDecimal constructor which takes double as argument.
🌐
Restack
restack.io › p › java-bigdecimal-to-string-answer
Where product teams design, test and optimize agents at Enterprise Scale — Restack
The open-source stack enabling product teams to improve their agent experience while engineers make them reliable at scale on Kubernetes.
🌐
Blogger
javahungry.blogspot.com › 2023 › 02 › format-bigdecimal-as-string-with-max-2-decimal-digits.html
Format BigDecimal as String with max 2 decimal digits | Java Hungry
We can easily format BigDecimal as String to exactly 2 decimal digits with the help of DecimalFormat as shown below in the examples: Example 1: Using new DecimalFormat("0.00") import java.math.BigDecimal; import java.text.DecimalFormat; public class FormatBigDecimal { public static void main(String ...
🌐
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - double value = 4.2352989244d; assertThat(String.format("%.2f", value)).isEqualTo("4.24"); assertThat(String.format("%.3f", value)).isEqualTo("4.235"); In Java, we have two primitive types that represent decimal numbers, float and decimal: double myDouble = 7.8723d; float myFloat = 7.8723f; The number of decimal places can be different depending on the operations being performed. In most cases, we’re only interested in the first couple of decimal places. Let’s take a look at some ways to format a decimal by rounding. The BigDecimal class provides methods to round to a specified number of decimal places.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
April 21, 2026 - A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used). Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).
Find elsewhere
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
January 15, 2025 - import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double value = 12.34567; int decimalPlaces = 2; BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScal...
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - In Java, we can use DecimalFormat("0.00"), String.format("%.2f") or BigDecimal to display double in 2 decimal places.
🌐
Tutorialspoint
tutorialspoint.com › java › math › bigdecimal_tostring.htm
Java.math.BigDecimal.toString() Method
... The following example shows the usage of math.BigDecimal.toString() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create a BigDecimal object BigDecimal bg; ...
🌐
Baeldung
baeldung.com › home › java › java numbers › truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
1 month ago - We can create a BigInteger by passing our double value into the constructor and directly telling it to keep two decimal places by rounding down at the same time: @Test void givenADouble_whenUsingBigDecimal_truncateToTwoDecimalPlaces(){ BigDecimal ...
🌐
Codementor
codementor.io › community › bigdecimal class in java
BigDecimal Class in Java | Codementor
June 23, 2024 - MathContext mc = new MathContext(3); BigDecimal bigDecimal = new BigDecimal("0.1", mc); System.out.println(bigDecimal); ... As a result, the displayed value has 2 digits after the decimal symbol.
🌐
Baeldung
baeldung.com › home › java › java numbers › convert double to string, removing decimal places
Convert Double to String, Removing Decimal Places | Baeldung
January 8, 2024 - The cast truncates the decimal part, meaning that it cuts it off without doing any rounding. This approach is about 10 times as fast as the other approaches we’ll look at. Once it’s an int, then we can then pass it to the valueOf method on the String class:
🌐
BeginnersBook
beginnersbook.com › 2024 › 05 › how-to-round-a-number-to-two-decimal-places-in-java
How to round a number to two decimal places in Java
May 31, 2024 - In this approach, there are two ...atValue)); First step is to convert the given double and float values to BigDecimal. This can be done using Double.toString() method. ... bdDouble = bdDouble.setScale(2, RoundingMode.HALF_UP); bdFloat = bdFloat.setScale(2, RoundingMode.H...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used). Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).
🌐
Coderanch
coderanch.com › t › 377763 › java › Big-Decimal-String
Big Decimal to String (Java in General forum at Coderanch)
September 9, 2005 - To get a string representation of a BigDecimal object, just call the toString() method (inherited from Object). If you need particular formatting, get the doubleValue() and format with java.text.DecimalFormat or use the various Formatter convenience methods if you're using 1.5.
🌐
How to do in Java
howtodoinjava.com › home › java basics › round off a float number to 2 decimals in java
Round Off a Float Number to 2 Decimals in Java
February 16, 2024 - double number = 4.56789; // 1. Using Math.round() double rounded = Math.round(number * 100.0) / 100.0; // 4.57 // 2. Using BigDecimal BigDecimal rounded = new BigDecimal(number).setScale(2, RoundingMode.HALF_UP); // 4.57 // 3. Using Apache Commons Math's Precision Class float rounded = Precision.round(number, 2, RoundingMode.ROUND_HALF_EVEN.ordinal()); // 4.57 // 4. Displaying a rounded off value DecimalFormat df = new DecimalFormat("###.##"); String rounded = df.format(number); //4.57 · Using BigDecimal class is the recommended approach for doing numerical operations in Java.