100

I am reading a currency from XML into Java.

String currency = "135.69";

When I convert this to BigDecimal I get:

 System.out.println(new BigDecimal(135.69));

Output:

135.68999999999999772626324556767940521240234375.

Why is it that it outputs this many numbers? How can I avoid this? All I want is for it to output 135.69.

4
  • You sure type of currency is string? And you're reading the value as string only? Commented Feb 28, 2015 at 16:21
  • possible duplicate of Set specific precision of a BigDecimal Commented Feb 28, 2015 at 16:21
  • 6
    Your currency is not even a String to start with; just use the BigDecimal constructor having a String as an argument. Commented Feb 28, 2015 at 16:24
  • There might be many solutions, but do you know why it happens? Read this if you don't: effbot.org/pyfaq/… Commented Feb 28, 2015 at 17:32

8 Answers 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:

  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.
Sign up to request clarification or add additional context in comments.

2 Comments

What would you say if you are in a service and were only being passed in a string. Note it could be an Integer, Double etc.
In that case I would use new BigDecimal ("string_value"). In this way, it would function correctly for both integer and decimal.
41
String currency = "135.69";
System.out.println(new BigDecimal(currency));

//will print 135.69

2 Comments

Not good practice to use constructor to instantiate "String", "BigInteger", "BigDecimal" and primitive-wrapper classes . rules.sonarsource.com/java/RSPEC-2129
@NateGetch (I know it is old) but have you read the link you provided? It itself is listing new BigDecimal("42.0") as compliant solution! The alternatives it states should be used, are autoboxing (does not work with BigDecimal) and valueOf() (no such method that takes a String)
6

Spring Framework provides an excellent utils class for achieving this.

Util class : NumberUtils

String to BigDecimal conversion -

NumberUtils.parseNumber("135.00", BigDecimal.class);

Comments

4

You are storing 135.69 as String in currency. But instead of passing variable currency, you are again passing 135.69(double value) into new BigDecimal(). So you are seeing a lot of numbers in the output. If you pass the currency variable, your output will be 135.69

Comments

3

May I add something. If you are using currency you should use Scale(2), and you should probably figure out a round method.

Comments

3

BigDecimal b = BigDecimal.valueOf(d);

import java.math.*; 

public class Test { 

    public static void main(String[] args) 
    { 

        // Creating a Double Object 
        Double d = new Double("785.254"); 

        /// Assigning the bigdecimal value of ln to b 
        BigDecimal b = BigDecimal.valueOf(d); 

        // Displaying BigDecimal value 
        System.out.println("The Converted BigDecimal value is: " + b); 
    } 
}

Comments

0

The answers here are not wrong, but not precise either. Your problem has nothing to do with the BigDecimal class as such, but with the nature of doubles.

A double is internally represented as an exponential, you could imagine the computer storing something looking like 0.22 * 2^15. However not every number can be represented accurately this way (for your case see: https://www.binaryconvert.com/result_double.html?decimal=049051053046054057). This inaccurate representation is what you pass to the BigDecimal constructor.

In conclusion, the input argument for your BigDecimal constructor is the approximation that you see in the end. The BigDecimal constructor did nothing wrong, its input was inaccurate.

Comments

-11

Hi Guys you cant convert directly string to bigdecimal

you need to first convert it into long after that u will convert big decimal

String currency = "135.69"; 
Long rate1=Long.valueOf((currency ));            
System.out.println(BigDecimal.valueOf(rate1));

1 Comment

(1) "you cant convert directly string to bigdecimal" - you sure can: new BigDecimal("135.69"); (2) "convert it into long" - that is not possible for a decimal number like 135.69

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.