Price synchronization with a currency symbol in Java

I want to parse the string that I have in Number. This is the code I'm using but not working:

NumberFormat.getCurrencyInstance(Locale.GERMAN).parse("EUR 0,00"); 

The result is java.text.ParseException

So, I want to combine String with a number, I really don't care about the currency, but it would be nice to have.

I need the following type of string:

 EUR 0,00 EUR 1.432,89 $0.00 $1,123.42 1,123.42$ 1,123.42 USD 

Of course, there are ways with RegEx, but I think it will be a kind of bust.

+4
source share
1 answer

Locale.GERMAN does not seem to have a currency symbol. Locale.GERMANY has the euro symbol as its currency (rather than the string "EUR"). Please note that blam1 and blam3 below lead to parsing errors, for the CurrencyFormat object like 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); } 

I suspect that you will either need to find an open source parser that suits you, or write it yourself.

+6
source

Source: https://habr.com/ru/post/1403697/


All Articles