Reputation: 292
I know this has been asked a lot of times, but I have honestly searched and I am still having problems making this work. I am not very good at coding as I am just starting to learn and teach myself. I have looked in the developer's library and all the answers on here and on Google, but I am having problems making this work. A lot of information I seem to come across is for older versions of IOS and Xcode. I need to have the number display just places after the decimal. For example, if the output is 90.909090, I need it to show 90.90 or 90.91. This works, but the output just shows 0.00, it is not reading the float. I know the output of this is just showing the NSString stringWithFormat:@"%.2f" in the weightkg text filed. I am having problems referencing the float to show in this format.
- (IBAction)calculateweight:(id)sender {
float wl = ([weightlbs.text floatValue]);
float wt = (wl/2.2);
weightkg.text = [[NSNumber numberWithFloat:wt] stringValue];
[weightkg setText:[NSString stringWithFormat:@"%.2f"]];
}
Upvotes: 2
Views: 8598
Reputation: 38012
You should be using:
float wt = ...;
weightkg.text = [NSString stringWithFormat:@"%.2f", wt];
That is, you must pass the variable value to your stringWithFormat
call.
Upvotes: 4
Reputation: 5732
You're not passing the float value to stringWithFormat, try this.
- (IBAction)calculateweight:(id)sender {
float wl = ([weightlbs.text floatValue]);
float wt = (wl/2.2);
[weightkg setText:[NSString stringWithFormat:@"%.2f", wt]];
}
Upvotes: 8