Double.valueOf(String). doubleValue()

Asked

Viewed 832 times

-2

Since 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 houses (1,000) are not stored in Double variable even with the above code, only numbers smaller than (1,000) are stored. How do I convert a String to decimal for calculations? ->Formatted field 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)
  • Double.valueOf or Double.parseDouble already do that, did not understand your doubt. Present a [mcve] to demonstrate your problem.

1 answer

2

In float and double types, by default, the point(.) is decimal separator, not thousands separator. If your string uses point as thousands separator, you won’t be able to convert properly to double, not before applying some type of formatting or removing these separators.

If you’re sure that the point will always be just thousands separator, simply remove it from the string before converting:

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

See working: https://ideone.com/f6u5h2

If you cannot guarantee this, edit the question and present examples of the types of numbers that may occur in your application.


Reading suggestion:

  • (Double.parseDouble(str.replaceAll(" .", "")) only didn’t work because the values you have to store are monetary, when you type "0.50" (fifty cents) it becomes "50.0" (fifty reais). I am working with a textbox formatted for monetary values.

  • 1

    @Rayra, how is the number format allowed in your field? Without knowing that I won’t be able to help you and we’ll be in this unproductive mess for you and me.

  • 1.111.111,11 <-- Maximum value. 0,00<-- Minimum value

  • Format according to typed values

  • @Rayra now became clearer, and his question has already been answered on the site, and I even suggested it in the links. See the link I marked in your question, read carefully the answer accepted and do as it explains.

Browser other questions tagged

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