How to switch CFA to EUR in my app?

I’m working on an app that needs to change CFA to EUR. It’s going okay when there’s no decimal point. Like if I put in 258782, it works fine. But when I add a decimal like 258782,52 nothing shows up. Here’s what I’ve got so far:

void switchCFAtoEUR() {
    float cfa = 0;
    float eur = 0;
    String euroStr = "";
    
    if (cfaInput.length() > 0) {
        convertBtn.setEnabled(true);
        try {
            String cfaText = cfaInput.getText().toString();
            String fixed = cfaText.replace(",", ".");
            cfa = Float.parseFloat(fixed);
            eur = cfa / 654.67f;
            euroStr = String.valueOf(eur);
            eurOutput.setText(formatDecimal(euroStr));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Any ideas on how to fix this? I’m stuck and could use some help!

I encountered a similar issue when working on a currency conversion feature. One thing that helped me was using DecimalFormat for precise formatting. Here’s a snippet that might be useful:

DecimalFormat df = new DecimalFormat("#,##0.00");
df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
String formattedEur = df.format(eur);
eurOutput.setText(formattedEur);

This ensures consistent decimal formatting regardless of locale settings. Also, consider using BigDecimal instead of float for more accurate calculations with monetary values. It might solve your decimal point problem.

Lastly, double-check your input handling. Make sure you’re properly parsing the CFA input, especially if it uses commas as decimal separators in some locales.

Hey Nate_45Guitar! I’ve been tinkering with currency conversions too, and I totally get your frustration. Have you considered using BigDecimal instead of float? It’s way more precise for handling money.

Also, I’m curious - are you sure the issue is with the decimal point and not something else? Maybe try logging the values at each step to see where it’s breaking down.

What kind of formatting are you doing in the formatDecimal method? That could be causing issues too if it’s not handling decimals correctly.

Oh, and just a thought - are you accounting for different locales? Some countries use commas instead of periods for decimals. Might be worth looking into NumberFormat for that.

Keep us posted on how it goes! Currency stuff can be tricky, but I’m sure you’ll crack it soon. :blush:

yo nate, i had similar issues. try usin BigDecimal instead of float. it’s better for money stuff. also, check ur formatDecimal method. it might be messin things up with the decimals. keep us updated on how it goes, currency conversion can be a pain!