In Android how can I change a value to a negative number?
If I have the following code and have entered 10 for pos1_deg
, how can I easily change this to -10 using a radio button?
Is the answer to concatenate a string such as pos1_deg = "-" + pos1_deg
? Or is there some mathematical command I can use?
pos1_deg =(EditText)findViewById(R.id.sat1posdeg);
pos1_deg.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
开发者_Python百科
Many thanks for any help
As the others suggested you can use that way to convert a positive to a negative but what if you have a negative instead of a positive(i don't know your requirement) so better use :
Math.abs(pos1_deg) and use any of above techniques e.g
Math.abs(po1_deg) * -1
0 - Math.abs(pos1_deg)
so you won't get any error calculations...
Multiply pos1_deg
by -1
:
pos1_deg * -1
Just subtract from zero to invert the sign.
0 - pos1_deg
public void onProgressChanged(SeekBar seekBar, int progress1,
boolean fromUser)
{
{
// TODO Auto-generated method stub
value2.setText("Right Contact:"+B);
value3.setText("Delta:"+(A-B));
B = -1*progress1 ;
}
answer
from api 24 you can use
Math.negateExact(pos1_deg)
pos1_deg is your int
精彩评论