ios - 如何从TextField中获取十进制值并在Objective C中进行计算

标签 ios objective-c uitextfield

我对 objective-c 真的很陌生,我在文本字段及其值方面遇到了一些问题。我的ViewController中有两个TextField。有了这些TextField,我试图获得用户的体重和身高,并计算他们的体重指数。我已经在android中编写了以下代码,并尝试将其转换为 objective-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)+"");

我的问题是;
  • 如何从文本字段(键盘类型为Decimal Pad)中获取值并将其转换为十进制数。
  • 如何在 objective-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];
    }
    

    关于ios - 如何从TextField中获取十进制值并在Objective C中进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48547864/

    相关文章:

    ios - 我想在时间格式为 hh :mm:ss 的标签上显示当前时间

    iphone - 在 textFieldDidEndEditing 中隐藏 UITextField

    ios - 自定义 UITableViewCell : UITextFields clearing when scrolled out of view- dequeuing problems 的问题

    iOS 9.3 部署目标

    ios - 无法在 swiftUI 的剪辑形状中使用旋转效果

    ios - NSAutoLayoutConstraints 崩溃的应用程序

    iOS - 导航 Controller 后退按钮

    iOS UITextField 值,无需点击 RETURN 按钮

    iphone - 如何让2个UILabels的文本匹配?

    iphone - 如何摆脱 iPhone 指针 uint8_t 警告?