-2

i have somewhere float USD = 1.3032;

and a function with a parameter (NSString*)currency and currency = @"USD";

i want to do -> float f = 100 * currency

i want to match the currency NSString to the float @"USD" -> float USD

Thanx

4

2 回答 2

5

不,您需要将货币名称和汇率存储在 NSDictionary 中。

例如,如果您创建了一个字典

NSDictionary *euroExchangeRates = @{
   @"USD" : @1.3032F, 
   @"CAD" : @1.36F,
   @"GBP" : @0.86F
};

并且您想f包含 100 欧元的美元,您可以使用:

float f = 100.00 * [euroExchangeRates[@"USD"] floatValue];

注意:该示例使用新的Objective-C 文字和下标语法

于 2013-07-12T21:31:53.817 回答
0

我不太确定您在问什么,但是如果您尝试将 NSString 转换为浮点数,则格式为

[@"yourstring floatValue];
于 2013-07-12T21:32:24.520 回答