【发布时间】:2014-03-04 05:46:20
【问题描述】:
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];
感谢大家的帮助!尤其是许音!!
【问题讨论】:
-
每当您在stackoverflow上寻求答案时,您都需要添加更多信息。
-
上次我被告知我必须提供很多信息,但不是直截了当....
-
将
int Bonus更改为float Bonus否则您的结果将始终为 0。
标签: objective-c uitextfield int uilabel decimal