[英]How to get decimal value from TextField and make calculations with it in Objective C
Im really new to objective c and I faced some problems with the Textfields and their values. 我对目标c很新,我遇到了Textfields及其值的一些问题。 I have two TextFields in my ViewController.
我的ViewController中有两个TextField。 With those TextFields Im trying to get the weight and height of the user and calculate their Body Mass index.
使用那些TextFields我试图获得用户的体重和身高并计算他们的体重指数。 I already wrote the code below in android and try to convert it to objective c .
我已经在android中编写了下面的代码,并尝试将其转换为目标c。
valueWeight = Double.parseDouble(edit_txt_weight.getText().toString());
valueHeight = Double.parseDouble(edit_txt_height.getText().toString());
resulBMI = (valueWeight / (valueHeight * valueHeight)*10000);
txt_vki_resultText.setText(new DecimalFormat("##.##").format(resulBMI)+"");
My Questions are; 我的问题是;
How do I get the values from the Textfields (Keyboard type is Decimal Pad) and convert them to decimal numbers. 如何从Textfields获取值(键盘类型为Decimal Pad)并将它们转换为十进制数。
How to make the calculations in objective c, I get some problems when I get the values from Textfields with this code 如何在目标c中进行计算,当我使用此代码从Textfields获取值时,我遇到了一些问题
- (IBAction)btnCalculate:(id)sender { NSString *weight = self.editTextKg.text; NSString *height = self.editTextHeight.text; }
- (IBAction)btnCalculate:(id)sender {
double weight = [ self.editTextKg.text doubleValue]
double height = [ self.editTextHeight.text doubleValue];
double resulBMI = (valueWeight / (valueHeight * valueHeight)*10000);
txt_vki_resultText.text = [NSString stringWithFormat:@"%.2f",resulBMI];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.