| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 897 人关注过本帖
标题:怎么把小数点后保留两位
取消只看楼主 加入收藏
lansite2006
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-9-10
收藏
 问题点数:0 回复次数:2 
怎么把小数点后保留两位

谁能告诉我一下,谢谢!!

搜索更多相关主题的帖子: 小数点 
2006-12-31 20:46
lansite2006
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-9-10
收藏
得分:0 

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不能小数点后面保留两位!!版主帮我看看我哪错了

2006-12-31 21:14
lansite2006
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-9-10
收藏
得分:0 

不保留的

2006-12-31 22:37
快速回复:怎么把小数点后保留两位
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017764 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved