Reputation: 11
I'm very new to android programming so any help would really be appreciated.I have gone through the pages and do not seem to find the right answer. If this is a similar question I honestly do apologize. My problem is that when I wrote the code on my android device with AIDE compiler the rounding of example below
pitch_round.setScale(2,BigDecimal.ROUND_UP);
worked fine and I was able to output the rounded bigdecimal to a TextView by using pitch_round.ToString() , when I used similar code in Eclipse there wasn't an error in compiling but the ouput to textview did not seem to round at all , if i am not mistaken there seems to be more decimal characters now.
Below is a snippet of the code that is not working.Is there something that I am not doing right here? All falues are floats This is identical to the code that was used on AIDE on the device Thank you in advance
pitch = mOrientation[1]*57.2957795f;
roll= mOrientation[2]*57.2957795f;
yaw= mOrientation[0]*57.2957795f;
// change to bigdecimal object
BigDecimal pitch_round = new BigDecimal(pitch-zero_pitch);
BigDecimal roll_round = new BigDecimal(roll-zero_roll);
BigDecimal yaw_round = new BigDecimal(yaw-zero_yaw);
pitch_round.setScale(2,BigDecimal.ROUND_UP);
roll_round.setScale(2,BigDecimal.ROUND_UP);
yaw_round.setScale(2,BigDecimal.ROUND_UP);
Log.i(SENSOR_SERVICE, pitch_round.toString());
t_pitch.setTextColor(Color.CYAN);
t_roll.setTextColor(Color.CYAN);
t_yaw.setTextColor(Color.CYAN);
// log some output to test code
//Log.i(SENSOR_SERVICE, "Send Results"+ pitch);
t_pitch.setText(pitch_round.toString());
t_roll.setText(roll_round.toString());
t_yaw.setText(yaw_round.toString());
Upvotes: 1
Views: 365
Reputation: 31689
setScale returns a new BigDecimal, which you're discarding. Try
pitch_round = pitch_round.setScale(2,BigDecimal.ROUND_UP);
From the setScale API doc:
Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.
Upvotes: 1
Reputation: 240900
Change all like
pitch_round.setScale(2,BigDecimal.ROUND_UP);
to
pitch_round = pitch_round.setScale(2,BigDecimal.ROUND_UP);
BigDecimal
is immutable, calls of this method do not result in the original object being modified
Upvotes: 4