BigDecimal doesn't represent a rendering. In other words, whether to use a comma or a dot as separator is not part of the properties a BigDecimal object has.

Hence, you do not want to call .replaceAll. (And separately, you'd want .replace(".", ",") - replace replaces all, and replaceAll also replaces all and interprets the first arg as a regex, and is therefore needlessly confusing here). Just pass it with the dot.

To render a BigDecimal, don't just sysout it, that will always show a dot and there is nothing you can do about that. toString() is almost never the appropriate tool for the job of rendering data to a user - it's a debugging aid, nothing more. Use e.g. String.format("%f"), specifying the appropriate locale. Or use NumberFormat. The javadoc of BigDecimal explicitly spells this out.

There are various other issues with your code:

  • "cast" is the technical name for the syntactic construct: (Type) expr; - and this construct does 3 utterly different things, hence using it to describe a task, i.e. use it in a method name, is a very bad idea. In particular, only one of the 3 things it does converts anything, and you clearly use it here in the 'convert something' meaning. This is misleading; only if it's all primitives does the cast operator convert, and BigDecimal isn't primitive. Call it convertTo or whatever you please, not "cast".
  • BigDecimal is an extremely complicated tool for the job and usually not the right tool if you want to represent financial data. Instead, represent the atomary unit in a long and call the appropriate rendering method whenever you need to show it to a user. For example, for euros, the atomary unit is the eurocent. If something costs €1,50, you'd store "150", in a long. Before you think: But, wait, I want to divide, and then I'd lose half a cent! - yes, well, you can't exactly send your bank a request to transfer half a cent, either. Also, try to divide 4 cents by 3 with a BigDecimal and see what happens. Dividing financial amounts is tricky no matter what you use, BD isn't a catch-all solution to this problem.
Answer from rzwitserloot on Stack Overflow
Top answer
1 of 2
1

BigDecimal doesn't represent a rendering. In other words, whether to use a comma or a dot as separator is not part of the properties a BigDecimal object has.

Hence, you do not want to call .replaceAll. (And separately, you'd want .replace(".", ",") - replace replaces all, and replaceAll also replaces all and interprets the first arg as a regex, and is therefore needlessly confusing here). Just pass it with the dot.

To render a BigDecimal, don't just sysout it, that will always show a dot and there is nothing you can do about that. toString() is almost never the appropriate tool for the job of rendering data to a user - it's a debugging aid, nothing more. Use e.g. String.format("%f"), specifying the appropriate locale. Or use NumberFormat. The javadoc of BigDecimal explicitly spells this out.

There are various other issues with your code:

  • "cast" is the technical name for the syntactic construct: (Type) expr; - and this construct does 3 utterly different things, hence using it to describe a task, i.e. use it in a method name, is a very bad idea. In particular, only one of the 3 things it does converts anything, and you clearly use it here in the 'convert something' meaning. This is misleading; only if it's all primitives does the cast operator convert, and BigDecimal isn't primitive. Call it convertTo or whatever you please, not "cast".
  • BigDecimal is an extremely complicated tool for the job and usually not the right tool if you want to represent financial data. Instead, represent the atomary unit in a long and call the appropriate rendering method whenever you need to show it to a user. For example, for euros, the atomary unit is the eurocent. If something costs €1,50, you'd store "150", in a long. Before you think: But, wait, I want to divide, and then I'd lose half a cent! - yes, well, you can't exactly send your bank a request to transfer half a cent, either. Also, try to divide 4 cents by 3 with a BigDecimal and see what happens. Dividing financial amounts is tricky no matter what you use, BD isn't a catch-all solution to this problem.
2 of 2
-1

I looked up the source code for Java 8's implementation of BigDecimal (https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/java/math/BigDecimal.java), and the period character is hard-coded in that source as the decimal point. I would not have thought this of a language for which internationalization has been so thoroughly designed in, but there it is, line 466.

Given that the author(s) of BigDecimal failed to take locale into account in such a basic way -- the use of comma instead of period as the decimal separator in Europe is well-known -- I'd have to say you cannot use that BigDecimal constructor on unaltered Strings that are otherwise formatted correctly but which (might) have a comma separator. There are other options -- the previous SO post referred to in one of the comments has one -- but it appears you cannot convert your String this way.

(One minor point -- you are not "casting" anything. That word has a specific meaning in OO programming, and a more specific one in Java, and has very little to do with your question. It is incorrect to refer to conversion as casting.)

🌐
Coderanch
coderanch.com › t › 385788 › java › converting-String-Bigdecimal
Problem while converting String to Bigdecimal (Java in General forum at Coderanch)
You must use a decimal point, not a comma, to separate the integer and fractional parts of the value passed to BigDecimal. Since you can�t use the new DecimalFormat support, you�ll have to replace the comma by a decimal point before converting the string to a BigDecimal.
Discussions

java - Safe String to BigDecimal conversion - Stack Overflow
I'm trying to read some BigDecimal values from the string. Let's say I have this String: "1,000,000,000.999999999999999" and I want to get a BigDecimal out of it. What is the way to do it? First o... More on stackoverflow.com
🌐 stackoverflow.com
formatting - Java format BigDecimal numbers with comma and 2 decimal place - Stack Overflow
I want to format BigDecimal Numbers with comma and 2 decimal points. e.g. Amount is: 5.0001 and formatted to: 5.00 Amount is: 999999999.999999 and formatted to: 999,999,999.99 Amount is: 1000.4999 and formatted to: 1,000.49 Amount is: 9999.089 and formatted to: 9,999.08 Amount is: 0.19999 and formatted to: 0.19 Amount is: 123456.99999999 and formatted to: 123,456.99 ... This should be enough to get the results. String... More on stackoverflow.com
🌐 stackoverflow.com
decimal - Java BigDecimal can have comma instead dot? - Stack Overflow
When I update the decimal value ... it with a number like 100,23 the code throws an exception. Why is that? ... Please post your actual code and the "dump". chrylis -cautiouslyoptimistic- – chrylis -cautiouslyoptimistic- 2013-09-10 08:47:49 +00:00 Commented Sep 10, 2013 at 8:47 ... The javadocs of BigDecimal are pretty clear on what format is allowed; docs.oracle.com/javase/6/docs/api/java/math/… ... BigDecimal doesn't have a dot or a comma. You can parse a String which contains ... More on stackoverflow.com
🌐 stackoverflow.com
java - How can I parse a String to BigDecimal? - Stack Overflow
Try the correct constructor ...ml#BigDecimal(java.lang.String) You can directly instanciate the BigDecimal with the String ;) ... BigDecimal can not parse the example string. 2016-09-08T14:42:14.627Z+00:00 ... It will fail for the string value: 1,000 commas are not taking ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
tanutaran.medium.com › java-format-bigdecimal-with-comma-and-dot-cc2b33cbaccd
Java Format BigDecimal with Comma and Dot - tanut aran - Medium
September 8, 2024 - Java Format BigDecimal with Comma and Dot It is common to format BigDecimal to the accounting format like below 10000.02 --> 10,000.02 21231231.11 --> 21,231,231.11 Just like double or other format …
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
June 17, 2026 - Using NumberFormat.getCurrencyInstance(Locale.FRANCE) applies these formatting rules automatically, allowing the parser to interpret “348,45 €” correctly and produce the expected BigDecimal value of 348.45. By relying on locale-specific NumberFormat instances, we can accurately handle currency strings from different regions, ensuring reliable numeric conversion in internationalized applications. In this article, we learned that Java provides us with multiple methods to convert String to BigDecimal values.
Find elsewhere
🌐
Javathinking
javathinking.com › blog › convert-string-with-comma-to-bigdecimal-java
Convert String with Comma to BigDecimal in Java | JavaThinking.com
July 11, 2025 - Before converting the string to a BigDecimal, remove all commas from the string. You can use the replaceAll method to achieve this. If the input data might come from different locales, use a NumberFormat object to parse the string correctly. This ensures that the thousands separators and decimal ...
🌐
Java2s
java2s.com › example › java › java.math › insert-commas-to-bigdecimal-format.html
insert Commas to BigDecimal format - Java java.math
j a va 2 s. c o m public static String insertCommas(String number) { return insertCommas(new BigDecimal(number)); } } ... extremely simple version natural logarithm, using no algorithm tricks, but just via doubles and recreating BigDecimal Might give less accurate results, but this is to be tested.
🌐
Java2Blog
java2blog.com › java-string-to-bigdecimal
Java String to BigDecimal – Java2Blog
package org.arpit.java2blog; import java.math.BigDecimal; public class StringToBigIntegerMain { public static void main(String[] args) { String currency="13.23"; BigDecimal bigDecimalCurrency=new BigDecimal(currency); System.out.println("Converted String currency to bigDecimalCurrency: "+bigDecimalCurrency); } } When you run above program, you will get below output: Converted String currency to bigDecimalCurrency: 13.23 · that’s all about converting String to BigDecimal.
🌐
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - Notice the space character at the start of the formatted string when the number of characters is less than the specified minimum width of 12 characters. DecimalFormat is one of the most popular ways to format a decimal number in Java.
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › Convert-a-string-to-Big-Decimal › td-p › 2348714
Solved: Convert a string to Big Decimal - Qlik Community - 2348714
February 28, 2023 - Note that you may also need to handle any potential exceptions that can be thrown by the `BigDecimal` constructor, such as `NumberFormatException` if the input string is not a valid decimal number. SkyWard Alpine Login ... Works for me! ... You can use the `BigDecimal` class in Java to convert the string "$6.00" to a `BigDecimal` object.
🌐
Blogger
javahungry.blogspot.com › 2020 › 01 › string-to-bigdecimal.html
2 ways to Convert String to BigDecimal in Java with Examples | Java Hungry
The first step is to convert the String to Double. The second step is to convert Double to BigDecimal, using BigDecimal.valueOf(double) method. As shown in the example below: import java.math.*; public class JavaHungry { public static void main(String args[]) { String str = "123.45"; // Converting ...
🌐
Java Guides
javaguides.net › 2019 › 07 › java-conversion-between-bigdecimal-and-string-tutorial.html
How to Convert String into BigDecimal in Java
June 15, 2024 - ... The simplest way to convert a String to a BigDecimal is by using the BigDecimal constructor that takes a String as an argument. This constructor provides exact conversion without loss of precision.
Top answer
1 of 8
185

The BigDecimal(double) constructor can have unpredictable behaviors. It is preferable to use BigDecimal(String) or BigDecimal.valueOf(double).

Use with AISystem.out.println(new BigDecimal(135.69)); //135.68999999999999772626324556767940521240234375
System.out.println(new BigDecimal("135.69")); // 135.69
System.out.println(new BigDecimal("13۵.۶9")); // 135.69
System.out.println(BigDecimal.valueOf(135.69)); // 135.69

The documentation for BigDecimal(double) explains in detail:

  1. The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
  2. The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
  3. When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
2 of 8
41
Use with AIString currency = "135.69";
System.out.println(new BigDecimal(currency));

//will print 135.69
🌐
Jaspersoft Community
community.jaspersoft.com › home › forum › how to convert value 1000.00 to 100,00.00?
How to convert value 1000.00 to 100,00.00? - Products - Jaspersoft Community
September 13, 2019 - I already try to change the pattern format for the number and use some pattern expression but nothing happen .. ELPP