This is my first time building an iOS project using any kind of advanced math. I have searched for quite a while but have yet to find anything to fix my problem. I am still quite new to iOS so any help would be greatly appreciated.
I am trying to use a basic tan() function that takes an input from a textfield, turns it into degrees (because tan() defaults to radians) and displays in a label. If I was to input tan 30 into a calculator it would return .577, I am getting .449, which is close but if I input 31, i receive a value of “2.356”?
Here is the part of my code:
-(IBAction)calculate {
float x = ([textField1.text intValue]);
float d = (x * 180) / M_PI;
float y = tan(d);
label.text = [[NSString alloc] initWithFormat:@"%2.3f", y];
}
The format string you’re using (
%2.f) is inappropriate for the data — it’s specifying that no decimal places should be displayed. Try just using%f.