How to change CFA currency to Euros?

Hey everyone! I’m working on a money converter app and I’ve hit a snag. My app works fine when I enter whole numbers, but it breaks when I add decimals.

For instance, if I type 258782, it converts correctly. But if I put in 258782,52 (using a comma for the decimal), it doesn’t show anything. Any ideas on how to fix this?

Here’s a simplified version of my code:

public void switchFromCFAtoEUR() {
    double francs = 0;
    double euros = 0;
    String result = "";
    
    if (inputFieldCFA.length() > 0) {
        try {
            String cfaAmount = inputFieldCFA.getText().toString();
            String fixed = cfaAmount.replace(",", ".");
            francs = Double.valueOf(fixed);
            euros = francs / 654.67;
            result = String.valueOf(euros);
            outputFieldEUR.setText(formatResult(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Any help would be awesome! Thanks in advance!

hey exploringforest, i’ve dealt with similar stuff. have u tried using NumberFormat? it’s pretty good for handling different number formats:

NumberFormat format = NumberFormat.getInstance();
Number number = format.parse(cfaAmount);
francs = number.doubleValue();

this should work for both commas and periods. also, maybe use BigDecimal instead of double for money stuff? just a thought. good luck with ur app!

I’ve encountered a similar issue when working on financial software. The problem likely stems from locale-specific decimal formatting. Your replace() method is a good start, but it might not cover all cases.

Consider using NumberFormat instead. It’s more robust for parsing locale-specific numbers:

NumberFormat format = NumberFormat.getInstance(Locale.getDefault());
Number number = format.parse(cfaAmount);
francs = number.doubleValue();

This approach should handle both comma and period separators automatically. Also, for currency calculations, BigDecimal is preferable to double for its precision. You might want to refactor your code to use BigDecimal throughout.

Lastly, ensure your input validation covers all possible user inputs to prevent unexpected crashes. Hope this helps!

Hey there ExploringForest! :wave:

Ah, the classic decimal dilemma! I’ve been there too. Your code looks pretty solid, but I think I might have a solution for you.

Have you considered using a DecimalFormat instead of Double.valueOf()? It’s super handy for handling different locale-specific number formats. Something like this might work:

DecimalFormat df = new DecimalFormat();
df.setParseBigDecimal(true);
BigDecimal francs = (BigDecimal) df.parse(fixed);

This should handle both comma and period as decimal separators. Plus, BigDecimal is generally better for currency calculations to avoid those pesky floating-point errors.

Oh, and quick question - are you planning to support other currencies in your app? It’d be cool to see a multi-currency converter!

Let me know if this helps or if you need any more pointers. Happy coding! :blush: