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)
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.
🌐
Java2Blog
java2blog.com › home › conversion › java bigdecimal to string
Java convert BigDecimal to String
January 12, 2021 - Did you just expect 20.234 as output? I think Yes. But issue is when you use BigDecimal constructor which takes double as argument, double cannot represent 20.234 exactly. You need to either use String based constructor or use BigDecimal.valueOf method to solve this issue.
🌐
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 ...
🌐
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.
🌐
Restack
restack.io › p › java-bigdecimal-to-string-answer
Java BigDecimal To String Conversion | Restackio
However, if you need a specific format, you can use DecimalFormat to customize the output: import java.text.DecimalFormat; BigDecimal number = new BigDecimal("123.456789"); DecimalFormat df = new DecimalFormat("#.##"); String formatted = df.format(number); System.out.println(formatted); // ...
🌐
TutorialsPoint
tutorialspoint.com › set-decimal-place-of-a-big-decimal-value-in-java
Set Decimal Place of a Big Decimal Value in Java
To set decimal place of a BigDecimal value in Java, try the following code − · Live Demo · 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); } } Value 1 : 37578975587.876876989 Value 2 : 62567875598.976876569 Result = 37578975587.876 Result = 62567875598.97688 ·
🌐
Google Groups
groups.google.com › g › comp.lang.java.programmer › c › uor4n8vo4Qo
Quickest way to get 2 decimal places from BigDecimal?
I'm using Java 1.6. For the purposes of writing the "cents" from a BigDecimal in a special way on a JSP page, how do I extract only the two decimal places from a BigDecimal number? For example, if my number is ... I want to get the number "45" (the two numbers after the decimal place) into a string.
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
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...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - 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).
🌐
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).
🌐
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
September 24, 2025 - 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 ...
🌐
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; // create a String object String s; MathContext ...
🌐
BeginnersBook -
beginnersbook.com › home › java › 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 - setScale(2, RoundingMode.HALF_UP) sets the scale of the BigDecimal to 2 decimal places.RoundingMode.HALF_UP is the rounding mode that rounds towards “nearest neighbour” unless both neighbours are at equal distance, in which case it rounds up. For example, 2.555 becomes 2.56, and 2.554 becomes ...
🌐
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 - Learn how to convert a Double to a String while removing the fractional digits using a number of different approaches.
🌐
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.