2013-10-16 30 views
1

我有一個值£211,064.10和我要像211064.10轉換和我嘗試以下方法如何幣值分成正常整數

String value = "$107,990.67";  

String abc = value.replace(NumberFormat.getCurrencyInstance().getCurrency().getSymbol(), "");  
System.out.println(abc); 
System.out.println(abc.replace(",", ""));` 

但它僅適用於貨幣符號$沒有任何其他貨幣值。

+0

是的,因爲你問什麼系統的局部電流符號,而你的情況總是會'$'。由於系統永遠不會返回'€'或'£',因此您將永遠不會尋找/替換它們。 –

回答

5

使用String.replaceAll

String abc = value.replaceAll("[^0-9.]", ""); 
+0

可能要先驗證字符串。也許'value.matches(「($ |€|£)[0-9] {0,3}(,[0-9] {3})*(\\。[0-9] +)?」) '或類 – Cruncher

+0

謝謝devnull,所以現在你給了一些正則表達式和它的工作正常。你能分享一些鏈接學習正則表達式。 – Lucan

+1

@Lucan起點可能是[this](http://en.wikipedia.org/wiki/Regular_expression),[this](http://docs.oracle.com/javase/tutorial/essential/regex/)太。 – devnull