xcode IOS:文本字段和双值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6954178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: textfield and double value
提问by cyclingIsBetter
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"
我应该在文本字段中写一个欧元值。如果在这个文本字段中我写“10”我应该存储在一个双值“10.00”或者如果我写“10,1”我应该存储在一个双“10,10”或者如果我写“10,23”我应该存储“10,23”
How can I do it?
我该怎么做?
回答by Serhii Mamontov
This should help:
这应该有帮助:
double val = [[textField text] doubleValue];
double val = [[textField text] doubleValue];
回答by gcbrueckmann
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]
:
如果你想支持本地化的数字(例如用户输入),-[NSNumberFormatter numberFromString:]
是要走的路。由于这会返回一个,NSNumber
您可以使用-[NSNumber doubleValue]
以下方法获取双精度值:
NSNumber *number = [numberFormatter numberFromString:inputString];
double doubleValue = [number doubleValue];
回答by Tendulkar
You can use the typecasting like convert string to double value.
您可以使用类型转换,例如将字符串转换为双精度值。