如何从文本字段中获取浮点值?

-1 floating-point objective-c uitextfield

我见过其他人提出类似的问题,但没有一个答案对我有用; 我收到消息"预期";' 在声明清单的末尾".有人可以引导我完成我应该做的事情吗?(这是我的第一个问题,所以如果我做错了,我很抱歉.)

Bhu*_*esh 11

float yourVariable = [yourTextField.text floatValue];
Run Code Online (Sandbox Code Playgroud)

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

如何将NSString值@"3.45"转换为float?

我还通过了如下链接它说 returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.