【发布时间】:2011-01-28 17:26:19
【问题描述】:
我有一个解析器返回一些字符串值,我想将其用作类实例初始化的参数。
我有一个方法询问两个 NSString 和一个浮点值,但我无法将字符串转换为浮点数,这是我的代码:
NSString *from = [[NSString alloc] initWithString:@"EUR"];
NSString *to = [[NSString alloc] initWithString:[attributeDict objectForKey:@"currency"]];
NSNumber *rate = [[NSNumber alloc] initWithFloat:[[attributeDict objectForKey:@"rate"] doubleValue]];
currency = [[Currency init] alloc];
[currency addCurrencyWithFrom:from andTo:to andRate:rate];
在.h
- (id) addCurrencyWithFrom: (NSString *) from_ andTo:(NSString *) to_ andRate:(float *) float_;
【问题讨论】:
-
but I can't convert the string to float- 你的意思是这里的字符串是 NSString 还是 C 字符串? -
附带说明,如果您正在做货币转换器,您可能有兴趣查看:github.com/davedelong/DDUnitConverter 它有一个从 IMF 提取数据的货币转换器。
-
还请注意,在使用货币做任何事情时,您真的很想避免浮动。浮点数在无法准确表示或计算相对较小的值方面非常出色。双打明显更好。
标签: objective-c nsstring floating-point nsnumber