Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField text to float (Objective-C)

How do I convert the text inside of a TextField to a float in Objective-C?

like image 928
Michael Anderson Avatar asked Dec 13 '22 00:12

Michael Anderson


2 Answers

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.

like image 188
Dave DeLong Avatar answered Dec 15 '22 14:12

Dave DeLong


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];

like image 30
catsby Avatar answered Dec 15 '22 15:12

catsby