Ste*_*fan 4 java parsing numbers currency
我想将一个字符串解析为一个数字.这是我正在使用但不工作的代码:
NumberFormat.getCurrencyInstance(Locale.GERMAN).parse("EUR 0,00");
Run Code Online (Sandbox Code Playgroud)
这会导致java.text.ParseException
所以我想把字符串匹配成一个数字,我真的不关心货币,但它会很高兴.
我希望以下类型的字符串匹配:
EUR 0,00
EUR 1.432,89
$0.00
$1,123.42
1,123.42$
1,123.42 USD
Run Code Online (Sandbox Code Playgroud)
当然,有很多方法可以使用RegEx,但我认为它有点矫枉过正.
Locale.GERMAN似乎没有货币符号.Locale.GERMANY将欧元符号作为其货币(不是字符串"EUR").请注意,下面的blam1和blam3导致解析异常,CurrencyFormat对象只喜欢blam2.
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.GERMANY);
System.out.println("75.13 euro: " + numberFormat.format(75.13));
try {
System.out.println("Parsed blam1: " + numberFormat.parse("EUR 75,11"));
} catch (ParseException exception) {
System.out.println("Parse Exception1: " + exception);
}
try {
System.out.println("Parsed blam2: " + numberFormat.parse("75,12 €"));
} catch (ParseException exception) {
System.out.println("Parse Exception2: " + exception);
}
try {
System.out.println("Parsed blam3: " + numberFormat.parse("€ 75,13"));
} catch (ParseException exception) {
System.out.println("Parse Exception3: " + exception);
}
Run Code Online (Sandbox Code Playgroud)
我怀疑您需要找到一个符合您需求的开源货币解析器,或者自己编写一个.