我是新來的目標c,我有很多格式爲「$ 99.99」(「$ double_number」)的字符串。 如何彙總所有價格並獲得一個實數?
0
A
回答
3
一個快速的解決方法是刪除$
,找到的doubleValue爲:
NSString *str = @"$99.99";
str = [str substringFromIndex:1];
double price = [str doubleValue];
NSLog(@"--> %.2f", price); //return 99.99
注:double
值(小數點後的值)具有更高的精度,你只需要兩個,我用.2f
。
2
NSString *stringValue = @"$99.99";
NSString *price = [stringValue substringFromIndex:1];
double price = [price doubleValue];
NSLog(@"price : %.2f", price);
說明:
首先,你想讓它刪除美元符號和明年將其轉換使用中的doubleValue
+0
你可以編輯答案來滿足他的要求,通過加上'.2f',因爲他想要兩位小數。 – 2014-11-24 13:38:43
1
檢查翻一番,如果字符串已使用NSStrings
hasPrefix
NSString *strWithDollar = @"$4.95";
if([strWithDollar hasPrefix:@"$"])
{
strWithDollar = [strWithDollar substringFromIndex:1];
}
double price = [strWithDollar doubleValue];
NSLog(@"price : %.2f", price); //return 4.95
相關問題
- 1. 將字符串轉換爲雙數
- 2. 如何將字符串與int轉換爲字符串與雙?
- 3. C#如何將雙轉換爲字符串指數符號
- 4. 將字符串轉換爲雙精度,
- 5. 將字符串轉換爲雙倍
- 6. 將字符串轉換爲雙倍
- 7. 將字符串轉換爲雙精度
- 8. jsoncpp:將字符串轉換爲雙倍
- 9. 轉換爲雙轉義字符串
- 10. 如何將字符串數組轉換爲字符串?
- 11. 如何將數據框轉換爲RDD [字符串,字符串]?
- 12. 如何將字符串轉換爲字符串數組String()?
- 13. 如何將非空字符串數組轉換爲字符串?
- 14. 將部分非數字字符串轉換爲雙倍
- 15. 如何將字符串轉換爲「雙引號」
- 16. 如何將6020494385.89982字符串值轉換爲雙精度值
- 17. 如何將非託管雙轉換爲託管字符串?
- 18. 如何將字符串從一個JTextField轉換爲雙陣列
- 19. 如何在C++中將字符串值轉換爲雙格式
- 20. 如何將百分比字符串轉換爲雙精度?
- 21. 如何將字符串列表轉換爲雙精度?
- 22. 在Python中如何將單個數字轉換爲雙數字符串?
- 23. 將字符串數組轉換爲雙數組
- 24. 如何將字符串的字符串轉換爲字符?
- 25. VB.NET - 將字符串轉換爲雙精度字符並返回
- 26. 如何將字符和字符串轉換爲字節數組?
- 27. C++字符串雙轉換
- 28. 將字符串轉換爲字符串
- 29. 如何將字符串值轉換(或轉換)爲整數值?
- 30. 如何將字符串[]轉換爲NameValueCollection?
前綴美元符號在整數或雙/浮動? – 2014-11-24 13:29:41
@Anoop Vaidya我想要雙倍從字符串中提取該值(例如「99.99美元」獲得99.99) – 2014-11-24 13:33:12