Double.valueOf (String) .doubleValue () [duplicate]

-2

As I could not convert a String to Double because it is decimal, I used Double.valueOf (String) .doubleValue (). But when converting a String to Double with "." the numbers above 3 boxes (1,000) are not stored in variable Double even with the above code, only numbers smaller than (1,000) are stored. How can I convert a String to decimal to decimal? -> field formatted for monetary values.

    Double vlprod=0.0;
    ArrayList<String> valorUnitario = new ArrayList();
    ArrayList<String> quantidade = new ArrayList();
     if (lstProduto.isSelectionEmpty()) /*Verifica se foi selecionado um item na JList*/
           { 
               JOptionPane.showMessageDialog(null, "Selecione um item na lista de produtos !");
           }
           else /*Se foi selecionado, pega o valor na posição do ArrayList referente ao índice da JList*/
           {
                int i= lstProduto.getSelectedIndex();
                Double vl=Double.valueOf(valorUnitario.get(i)).doubleValue();
                int qt=Integer.parseInt(quantidade.get(i));
                vlprod=Double.valueOf(vlprod+(vl*qt)).doubleValue();
                System.out.println(vlprod.toString());
           }

Erro no Console: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1914)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    
asked by anonymous 10.06.2018 / 20:05

1 answer

2

In float and double types, by default, the dot ( . ) is a decimal separator, not a thousands separator. If your string uses dot as thousands separator, you will not be able to convert properly to double, not without first applying some formatting or removing these tabs.

If you're sure the dot will always only be a thousands separator , simply remove it from the string before converting:

    String str = "1.000";   
    System.out.println(Double.parseDouble(str.replaceAll("\.", "")));

See it working: link

If you can not guarantee this, edit the question and give examples of the types of numbers that can occur in your application.

Suggested reading:

10.06.2018 / 21:00