Reputation: 1289
I'm learning programming for android and i made a calculator. The calculator is working fine but I'm having troubles with my sin, cos and tan formulas. I would like to display the values in degrees instead of radians.
Here is the piece of code that applies to that.
btnTan.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setValue(Double.toString(Math.tan(Double.parseDouble(txtCalc
.getText().toString()))));
}
});
And this is the function setValue
private void setValue(String value) {
if (operator == 0)
reset();
if (readyToClear) {
readyToClear = false;
}
txtCalc.setText(value);
txtCalc.setSelection(value.length());
hasChanged = true;
}
I would like to have another buttons that would toggle between radians and degrees like the Iphone calculator.
Thank you!
Upvotes: 0
Views: 6640
Reputation: 18151
double value = Math.tan(Math.toRadians(Double.parseDouble(txtCalc.getText().toString())));
value = ((double) Math.round(value * 1000)) / 1000;
setValue(Double.toString(value));
Upvotes: 6
Reputation: 7114
and
so you can adjust you variables accordingly
1 radian = 57.2957795 degrees
Upvotes: 2