I'm having some trouble rounding BigDecimals in java. I'm trying to convert a value from feet to inches and vice versa, this is what I have:
//To convert from ft to in;
//...
BigDecimal CINCH = new BigDecimal("12");
Inch.setText(CINCH.multiply(VFEET).toString()); //VFEET is the user input
//To convert from in to ft
//...
BigDecimal CFEET = new BigDecimal("12"); //Initially I multiplied the input with 0.0833333333
Feet.setText(VINCH.divide(CFEET, 7, RoundingMode.HALF_UP).toString()); //VINCH is the user input
If i try to convert 1 feet to inch, it returns the expected value (12). If i try to convert 12 inch to feet, it returns the expected value (1) but with 7 decimal cases like I expected.
If i convert 1 inch to feet, it returns 0.08333333 and if I convert this value from feet to inch, it was supposed to return 1, but instead, it returns 0.9999996. If i conver 2 inch to feet, it returns 0.1666667 and if I conver this value from ft to in, it returns 2.0000004
How can I fix this? (I know I could just do this with doubles, and that was my first plan but i ran in to an accuracy problem (bigger than this one))