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:
Answer from Marlon Patrick on Stack Overflow
- 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.
- 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.
- 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.
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).
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:
- 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.
- 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.
- 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
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
1 month ago - 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.
Videos
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
November 15, 2024 - We start by initializing a string variable `str` with the value "$6.00". We then create a `BigDecimal` object `bd` by passing the string `str` to the constructor of `BigDecimal`.
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
April 21, 2026 - Translates a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor, while allowing a sub-array to be specified and with rounding according to the context settings.
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 ...
Experts Exchange
experts-exchange.com › questions › 24899969 › Localized-String-to-BigDecimal.html
Solved: Localized String to BigDecimal | Experts Exchange
November 14, 2009 - You can use NumberFormat and use http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#getNumberInstance(java.util.Locale) to find out format to use for the locale. ... Sorry for the misconception, but I need to go the other way! So I'll give to example values: String nl_NL_BigDecimal = "343.298.723.948.729.384.0
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 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); } } }
GitHub
github.com › FasterXML › jackson-databind › issues › 5193
Serialize `BigDecimal` as a string instead of a floating-point number. · Issue #5193 · FasterXML/jackson-databind
June 16, 2025 - Reload to refresh your session. ... There was an error while loading. Please reload this page. ... There was an error while loading. Please reload this page. ... I searched in the issues and found nothing similar. I've discovered that Jackson serializes Java BigDecimal values as JSON numbers rather than strings...
Author FasterXML
Blogger
javahungry.blogspot.com › 2022 › 03 › bigdecimal-to-string.html
Convert BigDecimal to String in Java [2 ways] | Java Hungry
Using toString() method Read Also: Convert String to BigDecimal in Java
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.
Codemia
codemia.io › home › knowledge hub › convert string to double in java
Convert String to double in Java | Codemia
January 26, 2025 - This is fine for many scientific and general-purpose calculations, but not ideal for exact financial arithmetic. If exact decimal precision matters, parse into BigDecimal instead: ... 1import java.math.BigDecimal; 2 3BigDecimal amount = new BigDecimal("123.45"); 4System.out.println(amount);
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.
Coderanch
coderanch.com › t › 559104 › java › Big-Decimal-String
Big Decimal to String (Beginning Java forum at Coderanch)
November 17, 2011 - Rob Spoor wrote:Or create the BigDecimal from a String: Excellent advice. Must've had my brain turned off. Doesn't BD have also setScale() or somesuch method that controls the precision both for storage and for display? ... It even has three setScale methods. SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6 How To Ask Questions How To Answer Questions ... In Java 1.4, there are only 2 setScale methods: BigDecimal setScale(int scale) BigDecimal setScale(int scale, int roundingMode) Always best to use setScale(int, int).
Vaadin
vaadin.com › api › framework › current › com › vaadin › data › converter › StringToBigDecimalConverter.html
StringToBigDecimalConverter (vaadin-all 8.30.1 API)
You have to enable javascript in your browser to use this web site
Google Groups
groups.google.com › g › antlr-discussion › c › 9Fl_Fn75hNo
Java Concatenation, BigDecimal math.
July 1, 2014 - If left is a string literal, the '+' equates to exactly a '+'. If left is a number, the + is used as BigDecimal.add(BigDecimal).
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - String toString(): This method returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.