How do I convert the text inside of a TextField to a float in Objective-C?
float myFloat = [[myTextField text] floatValue];
Edit: technically the most proper way to do this would be to use an NSNumberFormatter
, since that will return a nil
NSNumber
if it can't parse the string properly, whereas floatValue
will just return 0.0 for a malformed string. But floatValue
is usually adequate for most purposes.
If it's an NSTextField
you call floatValue
on the actual object, not on what's returned from text
. NSTextField
does not have a text
method.
float myFloat = [myTextField floatValue];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With