Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONObject: difference between getBigDecimal and optBigDecimal #438

Closed
urfuchs opened this issue Oct 2, 2018 · 1 comment
Closed

JSONObject: difference between getBigDecimal and optBigDecimal #438

urfuchs opened this issue Oct 2, 2018 · 1 comment

Comments

@urfuchs
Copy link

urfuchs commented Oct 2, 2018

There is a difference between getBigDecimal and optBigDecimal.

JSONObject json = new JSONObject("{ \"key\" : 72.35 }");
BigDecimal bd1 = json.getBigDecimal(„key“);
BigDecimal bd2 = json.optBigDecimal(„key“, null);
System.out.println(bd1);
System.out.println(bd2);

prints:

72.35
72.349999999999994315658113919198513031005859375

Why is there a difference?

getBigDecimal converts the double to a String and than to a BigDecimal:
new BigDecimal(Double.valueOf(72.35).toString())
optBigDecimal converts the double directly to BigDecimal: new BigDecimal(Double.valueOf(72.35))

@borgogelli
Copy link

Same code, but the output for me is:

72.349999999999994315658113919198513031005859375
72.349999999999994315658113919198513031005859375

I use the 20200518 version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants