Check out setParseBigDecimal in DecimalFormat. With this setter, parse will return a BigDecimal for you.

Answer from Jeroen Rosenberg on Stack Overflow
🌐
Coderanch
coderanch.com › t › 410209 › java › Empty-BigDecimal
Empty for BigDecimal (Beginning Java forum at Coderanch)
Please advise me how do I handle this? Thanks in advance, Sakthi [ April 24, 2008: Message edited by: sakthi kannan reddiar ] ... You can't. From the BigDecimal(String) constructor javadoc · The string must contain at least one digit in either the integer or the fraction.
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
May 19, 2021 - 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.
🌐
Blogger
javahungry.blogspot.com › 2020 › 01 › string-to-bigdecimal.html
2 ways to Convert String to BigDecimal in Java with Examples | Java Hungry
BigDecimal obj = new BigDecimal(String); The above line will do the job. You can find the example below: import java.math.*; public class JavaHungry { public static void main(String args[]) { String str = "123.45"; // Using BigDecimal(String) constructor BigDecimal num = new BigDecimal(str); ...
🌐
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 BigDecimal constructor throws a NumberFormatException if the string does not represent a valid number. It is essential to handle this exception to ensure the robustness of your code. import java.math.BigDecimal; public class StringToBigDecimalWithExceptionHandling { public static void main(String[] args) { String strValue = "12345.6789a"; // Invalid numeric string try { // Attempt to convert string to BigDecimal BigDecimal bigDecimalValue = new BigDecimal(strValue); System.out.println("String value: " + strValue); System.out.println("BigDecimal value: " + bigDecimalValue); } catch (NumberFormatException e) { System.out.println("Invalid string format for BigDecimal: " + strValue); } } }
🌐
Java2Blog
java2blog.com › home › conversion › java string to bigdecimal
Java convert String to BigDecimal
January 12, 2021 - It is very simple to convert String to BigDecimal in java.
Top answer
1 of 8
184

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

System.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
String currency = "135.69";
System.out.println(new BigDecimal(currency));

//will print 135.69
Find elsewhere
🌐
Experts Exchange
experts-exchange.com › questions › 11456059 › How-to-convert-String-to-BigDecimal.html
Solved: How to convert String to BigDecimal. | Experts Exchange
July 17, 2000 - e, com.fw.biz.MetroService , java.math.*" , import javax.ejb.FinderException% > <HTML> <jsp:usebean id = "metrobean" Scope ="SessionScope" class ="com.fw.biz.MetroBean" /> <jsp:setProperty name = "metrobean" property = "*" /> <HEAD><TITLE>METROREGIONS </TITLE> </HEAD> <BODY> <% String s1 = request.getParameter("id") ; if (s1!=null){ BigDecimal id = null; String s2 = request.getParameter("desc
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.toString java code examples | Tabnine
/** * 人民币金额单位转换,元转换成分,例如:1 => 100 */ public static BigDecimal yuan2fen(BigDecimal y) { if (y != null) { return yuan2fen(y.toString()); } else { return new BigDecimal(0); } } ... /** * Return a string in floating point format.
🌐
GitHub
github.com › rails › rails › issues › 28738
String to BigDecimal for empty string throws exception · Issue #28738 · rails/rails
Steps to reproduce When tried to convert empty string to bigdecimal if throws exception' ArgumentError: invalid value for BigDecimal(): "" This is more related to ruby, I checked ruby code. This is...
🌐
Coderanch
coderanch.com › t › 385788 › java › converting-String-Bigdecimal
Problem while converting String to Bigdecimal (Java in General forum at Coderanch)
another option may be to use the NumberFormat class to parse the String into a Number. You should be able to construct a BigDecimal from the Number (or its primitive equivalent). The NumberFormat class has methods such as getXXXInstance with overloaded versions that will allow you to use a ...
🌐
Talend
community.talend.com › s › question › 0D53p00007vClLlCAK › resolved-string-to-bigdecimal-conversion
[resolved] String to Big-Decimal conversion
Qlik Community is the global online community for Qlik employees, experts, customers, partners, developers and evangelists to collaborate.