I do not get 2 decimals in the jTextField to pass it to a DB!

0

I'm going crazy and I do not know what to do, to see if someone can help me.

I would like that in the jTextField9 I would only get 2 decimals ( 123.45 ), because like this I get many decimals (123.45322222). because then I want the result with the 2 decimals to be added to the database with the SQL sequence, the most I could do is like this (123,45) but I do not want the comma and because it gives me error in the base of data.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BorrarJuny();
    try {
        Class.forName("org.sqlite.JDBC");
        Connection con = (Connection) DriverManager.getConnection("jdbc:sqlite:MyDataB.db");
        java.sql.Statement stmt = con.createStatement();
        String diaJuny = jTextField7.getText();
        double val2 = Double.parseDouble(jTextField8.getText());
        double val4 = Double.parseDouble(jTextField4.getText());
        double Multi = val2 * val4;
        String valorTotal = Double.toString(Multi);
        jTextField9.setText(valorTotal);
        String val3 = jTextField9.getText();
        String sql = "Insert into Juny values('" + (diaJuny) + "','" + (val2) + "','" + (val3) + "')";
        stmt.executeUpdate(sql);
    } catch (ClassNotFoundException | SQLException e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
    MostrarJuny();
    SumaJuny();
}

[UPDATE - SOLUTION] That's how it works for me, Thanks to Alberto and Christian!

private java.text.DecimalFormat formato = new java.text.DecimalFormat("0.00");

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BorrarJuny();
    try {
        Class.forName("org.sqlite.JDBC");
        Connection con = (Connection) DriverManager.getConnection("jdbc:sqlite:Manguan.db");
        java.sql.Statement stmt = con.createStatement();
        String diaJuny = jTextField7.getText();
        double val2 = Double.parseDouble(jTextField8.getText());
        double val4 = Double.parseDouble(jTextField4.getText());
        String val3 = formato.format(val2 * val4).replace(",", ".");
        String sql = "Insert into Juny values('" + (diaJuny) + "','" + (val2) + "','" + (val3) + "')";
        stmt.executeUpdate(sql);
    } catch (ClassNotFoundException | SQLException e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
    MostrarJuny();
    SumaJuny();
    
asked by Mekagenden 24.05.2018 в 15:20
source

3 answers

1

I would use a formatter.

private java.text.DecimalFormat formato =
                              new java.text.DecimalFormat("0.00"); 

Then I would use that formatter within the code:

double val2 = Double.parseDouble(jTextField8.getText());
double val4 = Double.parseDouble(jTextField4.getText());
String val3 = formato.format(val2 * val4);

Now that I would better use the JFormattedTextField instead of the JTextField because in the JFTF I can put the format that I require and in fact I would return a number instead of a String.

Si tus textFields fueran JFormattedTextFields entonces pudieras usarlos así:

double val2 = (double)jTextField8.getValue();
double val4 = (double)jTextField4.getValue();
String val3 = formato.format(val2 * val4);

I pass you leagues, in case you want to learn from these classes. Within them there are also links to tutorials.

link

link

    
answered by 24.05.2018 / 19:49
source
0

First of all the solution seems easy, I suppose it probably is, always the simplest thing is in the least it falls ...

You have this in your code:

    String valorTotal = Double.toString(Multi);
    jTextField9.setText(valorTotal);

Why do not you do:

    String valorTotal = Double.toString(Multi);
    valorTotal = valorTotal.replace(",",".");
    jTextField9.setText(valorTotal);

and then you pass it to

jTextField9

??

    
answered by 24.05.2018 в 15:27
0

First you set the value or you pass it to a variable the same thing:

String numDob = String.valueOf(Multi);

Then you will only cut the number as follows:

String numRecortado = "";
int x=0;
for(int i = 0; i < numDob.length(); i++){
   numRecortado = numRecortado + numDob.charAt(x);
   if(numDob.charAt(i).toString().equals(".")){
      i = numDob.length() - 2;
   }
   x++;
}

Since you get the new string then you can set it in your jtextfield

jTextField9.setText(numRecortado);
    
answered by 24.05.2018 в 18:03