【问题标题】:How do I get a float value from a text field?如何从文本字段中获取浮点值?
【发布时间】:2013-05-30 12:58:13
【问题描述】:

我看到其他人问过类似的问题,但没有一个答案对我有用;我收到消息“预期';'在声明列表的末尾”。有人可以指导我完成我应该做的事情吗? (这是我在这里的第一个问题,如果我做错了什么,我很抱歉。)

【问题讨论】:

  • 通常是一个好主意,显示代码并详细说明您认为哪里出了问题。让人们为您指明正确的方向。此外,有助于展示您自己所做的研究。
  • 并且不要对非 Xcode 问题使用“xcode”标签。

标签: objective-c floating-point uitextfield


【解决方案1】:
float yourVariable = [yourTextField.text floatValue];

确保向文本字段添加验证。如果您有任何非数字,它将返回 0。

How to convert NSString value @"3.45" into float?

我还关注了link,上面写着returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number

【讨论】:

    【解决方案2】:
    float floatsample = [sampletextfield.text floatValue];
    

    【讨论】:

      【解决方案3】:

      这样做:

      NSNumberFormatter *floatFormatter = [[NSNumberFormatter alloc] init];
      [floatFormatter numberFromString:textField.text];
      

      假设textField 是您的文本字段。然后你可以检查 floatFormatter 是否为 nil NSNumber 如果它不能正确解析字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多