开发者

IOS: textfield and double value

开发者 https://www.devze.com 2023-03-26 06:53 出处:网络
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 d

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消