谁能告诉我一下,谢谢!!
----------------解决方案--------------------------------------------------------
java.text.DecimalFormat
----------------解决方案--------------------------------------------------------
package passratio;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DBConnect extends JFrame {
JPanel contentPane;
JLabel lab1 = new JLabel();
JLabel lab2 = new JLabel();
JLabel lab3 = new JLabel();
JLabel lab4 = new JLabel();
JTextField txt1 = new JTextField();
JTextField txt2 = new JTextField();
JTextField txt3 = new JTextField();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
public DBConnect() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
lab1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
lab1.setToolTipText("");
lab1.setText("考试通过率统计");
lab1.setBounds(new Rectangle(141, 27, 221, 41));
lab2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lab2.setText("班级号");
lab2.setBounds(new Rectangle(42, 98, 66, 27));
lab3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lab3.setText("参考人数");
lab3.setBounds(new Rectangle(151, 98, 66, 27));
lab4.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lab4.setText("通过人数");
lab4.setBounds(new Rectangle(266, 98, 66, 27));
txt1.setText("");
txt1.setBounds(new Rectangle(28, 159, 76, 31));
txt2.setText("");
txt2.setBounds(new Rectangle(148, 158, 76, 31));
txt3.setText("");
txt3.setBounds(new Rectangle(260, 158, 76, 31));
btn1.setBounds(new Rectangle(22, 222, 91, 32));
btn1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
btn1.setText("输出数据");
btn1.addActionListener(new DBConnect_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(136, 221, 108, 32));
btn2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
btn2.setText("通过率查询");
btn3.setBounds(new Rectangle(276, 222, 63, 32));
btn3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
btn3.setText("关闭");
contentPane.add(lab1, null);
contentPane.add(lab2);
contentPane.add(lab3);
contentPane.add(lab4);
contentPane.add(txt1);
contentPane.add(txt2);
contentPane.add(txt3);
contentPane.add(btn3);
contentPane.add(btn2);
contentPane.add(btn1);
}
public void btn1_actionPerformed(ActionEvent e) {
//小数点后面精确两位
DecimalFormat DFormat=new DecimalFormat("0.00");
int classid=Integer.parseInt(txt1.getText());
if(txt1.getText().equals("")){
JOptionPane.showMessageDialog(this,"class_id不允许为空");
txt1.requestFocus();
return;
}
int total=Integer.parseInt(txt2.getText());
if(txt2.getText().equals("")){
JOptionPane.showMessageDialog(this,"total不允许为空");
txt2.requestFocus();
return;
}
int pass=Integer.parseInt(txt3.getText());
if(txt3.getText().equals("")){
JOptionPane.showMessageDialog(this,"pass不允许为空");
txt3.requestFocus();
return;
}
float ratio=((float)pass/(float)total);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:MyODBC");
Statement st=conn.createStatement();
String sql="insert into passratio values ('"+classid+"','"+total+"','"+pass+"','"+DFormat.format(ratio)+"')";
st.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"插入语句成功");
txt1.setText("");
txt1.requestFocus();
txt2.setText("");
txt3.setText("");
st.close();
conn.close();
}
catch(Exception ee){
System.out.println(ee);
}
}
}
class DBConnect_btn1_actionAdapter implements ActionListener {
private DBConnect adaptee;
DBConnect_btn1_actionAdapter(DBConnect adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
为什么ratio不能小数点后面保留两位!!版主帮我看看我哪错了
----------------解决方案--------------------------------------------------------
你把sql打印出来看一下,是不是保留了二位
----------------解决方案--------------------------------------------------------
不保留的
----------------解决方案--------------------------------------------------------