Double format with two house

Asked

Viewed 337 times

0

I’m treating a file .csv with a id, value (1,214.45) and I need to format a final sum of these values with only two boxes after the comma.

I developed the following code snippet:

`DecimalFormat df = new DecimalFormat("0.##");
    for(Integer i : filiais) {
        Double soma = 0.0;
        int contador = 0;
        for(String s : list) {
            String[] arr = new String[2];
            arr = s.split(",");
            if (arr[0].equals(i.toString())) {
                soma += Double.parseDouble(arr[1]);
                contador++;
            }
        }
        String aux = df.format(soma);
        somaFiliais.add(Double.parseDouble(aux));
        contadorFiliais.add(contador);
    }`

I have the following mistake:

Exception in thread "main" java.lang.NumberFormatException: For input string: "53010,77"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at Exercicio1.gerarRelatorio(Exercicio1.java:57)
    at Exercicio1.main(Exercicio1.java:26)
  • 3

    To use the Double.parseDouble you need to replace the comma with the point (in the variable aux)

  • direct in number? do not know how to do...

  • remembered, used the . replaceAll, thank you very much Valdeir!!

  • It was solved with the replaceAll following @Valdeirpsr’s suggestion, take advantage and put as a response so you can help future readers.

  • @A.Fernando, have a special reason to use Double? In general it is better to use Bigdecimal.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.