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.
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?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
permaculture is largely about replacing oil with people. And one tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps