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();