aspose file tools*
The moose likes Beginning Java and the fly likes BigDecimal wrong value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal wrong value?" Watch "BigDecimal wrong value?" New topic
Author

BigDecimal wrong value?

Jack Deli
Greenhorn

Joined: Jul 16, 2012
Posts: 2
When trying to make a simple converting program in Android I came across an issue with BigDecimal. My code below...

I try to multiply a number given by the user: for example 50.0 and I am supposed to get 1.64041994749999989200439642900164471939206123352050781250 but instead I get 164.0419947500..WHY? I do have a lot of if statements, meaby that's the problem? I tried searching the internet but couldn't find any solution to my problem.



Thanks in advance!
Blookiev123
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hi Jack,

You are using String constructor of BigDecimal which is good, if you want to get the expected result use double constructor.

number = number.multiply(new BigDecimal(0.03280839895));

Please go through documentation for more info on predictability of values passed to different constructors.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8176
    
  23

Jack Deli wrote:I try to multiply a number given by the user: for example 50.0 and I am supposed to get 1.64041994749999989200439642900164471939206123352050781250 but instead I get 164.0419947500..WHY?

I suspect that Harshvardhan is right; however, you are doing things properly and your answer is correct; so why would you imagine that you're 'supposed to get' a wrong answer?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
There is something not quite right about your if statements. I suspect you have a very large block of if-elses. I would suggest you create yourself a Map which contains the values. So you could have "Galileo→Foot/square second" mapping to new BigDecimal("0.03280839895"), etc. You would obviously have to populate the Map beforehand, but you are already creating all those objects, You would also have to use the values from the spinners to create the keys. You would also be able to block nonsense conversions like inch to kg/sqcm. In that case you would get null out of the Map.

And everybody is right: 1.6404199475 is in fact what you get from 50×0.03280839895. But you would appear to have acquired two extra 0s somewhere to get 164.0419947500! Try printing the number value before the calculations. Because you are doing arithmetic on number, it is conceivable that you are changing it to 5000 without noticing. And number is hardly a good name for a variable. Try changing to a two variables, maybe called input and result.

Are you actually using spinners to hold those values? I thought spinners only returned numbers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal wrong value?