int Bonus = .3(需要百分比等于30%)
label =(textfield * Bonus)
Objective-C的新手,如果需要进一步澄清,请等待。
更新:已解决!解决方案:
这是.h文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UILabel *label;
IBOutlet UITextField *textfield;
}
- (IBAction)save:(id)sender;
@end
这是.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id) sender {
float x = ([textfield.text floatValue]);
float bonus = .3;
float result;
result = x * bonus / 10;
// Divide by 10 to keep it percentage based
label.text = [[NSString alloc] initWithFormat:@"%.2f", result];
感谢大家的帮助!尤其是许吟!!
答案 0 :(得分:1)
我同意pacman321,也许下次会尝试添加更多信息..
看起来你的问题是:
//1.> "convert NSString to number"
float value = [textfield.text floatValue];
//2.> do the math
value = value * Bonus; //Could also be value *= Bonus;
//3.> Convert number back to NSString
label.text = [NSString stringWithFormat:@"%f", value];
我认为这就是你所需要的,但这里有一些建议:确保你的文本字段只有数值。并且可能需要注意您希望在标签中显示多少位数。
答案 1 :(得分:0)
textfield.text = [NSString stringWithFormat:@"%d%%", Bonus * 100]