IOS: textfield and double value
I have this situation:
I should write in a text field a value in euro. If inside this text field I write "10" I should store in a double value "10.00" or if I write "10,1" I should store in a double "10,10" or if I write "10,23" I should开发者_如何学编程 store "10,23"
How can I do it?
This should help:
double val = [[textField text] doubleValue];
If you want to support localized numbers (e. g. user input), -[NSNumberFormatter numberFromString:]
is the way to go. Since this returns an NSNumber
you can get the double value using -[NSNumber doubleValue]
:
NSNumber *number = [numberFormatter numberFromString:inputString];
double doubleValue = [number doubleValue];
You can use the typecasting like convert string to double value.
精彩评论