Question

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!

Was it helpful?

Solution

double value = Math.tan(Math.toRadians(Double.parseDouble(txtCalc.getText().toString())));
value = ((double) Math.round(value * 1000)) / 1000;
setValue(Double.toString(value));

OTHER TIPS

enter image description here and enter image description here

so you can adjust you variables accordingly

1 radian = 57.2957795 degrees
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top