Como analisar um valor da moeda (EUA ou UE) para flutuar o valor em Java

Na Europa, os decimais são separados por ', 'e usamos' @ opcion. 'para separar milhares. Eu permito valores de moeda com:

Notação 123,456.78 no estilo US Notação 123.456,78 de estilo europeu

Utilizo a próxima expressão regular (da biblioteca RegexBuddy) para validar a entrada. Permito frações opcionais de dois dígitos e separadores de milhares opcionai

^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\.[0-9]{0,2})?|(?:\.[0-9]{3})*(?:,[0-9]{0,2})?)$

Gostaria de analisar uma string de moeda em um float. Por exempl

123.456,78 deve ser armazenado como 123456.78
123.456,78 deve ser armazenado como 123456.78
123.45 deve ser armazenado como 123.45
1.234 deve ser armazenado como 1234 12.34 deve ser armazenado como 12.34

e assim por diante..

Existe uma maneira fácil de fazer isso em Jav

public float currencyToFloat(String currency) {
    // transform and return as float
}

Use BigDecimal em vez de Float

Obrigado a todos pelas ótimas respostas. Alterei meu código para usar BigDecimal em vez de float. Vou manter a parte anterior desta pergunta com float para impedir que as pessoas cometam os mesmos erros que eu ia fazer.

Soluçã

O próximo código mostra uma função que se transforma da moeda dos EUA e da UE em uma string aceita pelo construtor BigDecimal (String). Ou seja, uma string sem separador de milhar e um ponto para fraçõe

   import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TestUSAndEUCurrency {

    public static void main(String[] args) throws Exception {       
        test("123,456.78","123456.78");
        test("123.456,78","123456.78");
        test("123.45","123.45");
        test("1.234","1234");
        test("12","12");
        test("12.1","12.1");
        test("1.13","1.13");
        test("1.1","1.1");
        test("1,2","1.2");
        test("1","1");              
    }

    public static void test(String value, String expected_output) throws Exception {
        String output = currencyToBigDecimalFormat(value);
        if(!output.equals(expected_output)) {
            System.out.println("ERROR expected: " + expected_output + " output " + output);
        }
    }

    public static String currencyToBigDecimalFormat(String currency) throws Exception {

        if(!doesMatch(currency,"^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\\.[0-9]{0,2})?|(?:\\.[0-9]{3})*(?:,[0-9]{0,2})?)$"))
                throw new Exception("Currency in wrong format " + currency);

        // Replace all dots with commas
        currency = currency.replaceAll("\\.", ",");

        // If fractions exist, the separator must be a .
        if(currency.length()>=3) {
            char[] chars = currency.toCharArray();
            if(chars[chars.length-2] == ',') {
                chars[chars.length-2] = '.';
            } else if(chars[chars.length-3] == ',') {
                chars[chars.length-3] = '.';
            }
            currency = new String(chars);
        }

        // Remove all commas        
        return currency.replaceAll(",", "");                
    }

    public static boolean doesMatch(String s, String pattern) {
        try {
            Pattern patt = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
            Matcher matcher = patt.matcher(s);
            return matcher.matches();
        } catch (RuntimeException e) {
            return false;
        }           
    }  

}

questionAnswers(8)

yourAnswerToTheQuestion