[英]Convert string to float in Objective-C
如何在 Objective-C 中將字符串轉換為浮點數?
我正在嘗試乘以我從 JSON 返回的幾個字符串:
float subTotal = [[[[purchaseOrderItemsJSON objectAtIndex:i] objectAtIndex:j] objectForKey:@"Price"] floatValue];
NSLog(@"%@", subTotal);
這給了我: (null)
在控制台中。 我知道該數組中有一個有效的字符串,因為我已經使用它在標簽中顯示其值。
your_float = [your_string floatValue];
編輯:
嘗試這個:
NSLog(@"float value is: %f", your_float);
這樣做的正確方法是
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
float value = [numberFormatter numberFromString:@"32.12"].floatValue;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.