-1

Can some one help with a calculation please.

The following code is from two TextFields and inputs into a third one (endmonth1year1percentageTextField). When i put in the numbers (100 for the first and 145 for the second) I get an output of 1 instead of 1.45. What I really need is that 1.45 turned into a percentage as in .45.

float x = ([startmonth1year1TextField.text floatValue]);
float c = (([endmonth1year1TextField.text floatValue]) / x);
endmonth1year1percentageTextField.text = [[NSString alloc] initWithFormat:@"%2.f", c];

Any help is appreciated. Thanks

Scubadivingfool
  • 1,227
  • 2
  • 10
  • 23

1 Answers1

1

You need to do

endmonth1year1percentageTextField.text = [[NSString alloc] initWithFormat:@"%.2f", c];

instead of

endmonth1year1percentageTextField.text = [[NSString alloc] initWithFormat:@"%2.f", c];

(.2f instead of 2.f)

Bruno Koga
  • 3,864
  • 2
  • 34
  • 45