JAVA关于float类型的乘法的问题
两个JTextField文本框,一个文本框里写13.7,另一个文本框里写9,我把两个文本框里的字符串给转换成float类型的了,但是一相乘,结果为什么是123.29999...
两个JTextField文本框,一个文本框里写13.7,另一个文本框里写9,我把两个文本框里的字符串给转换成float类型的了,但是一相乘,结果为什么是 123.299995 啊?
结果正常应该是 123.3 啊,怎样才能得到123.3啊?
下边是我 的 代码:
package untitled1;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jTextField1.setText("13.7");
jTextField1.setBounds(new Rectangle(51, 96, 77, 20));
jButton1.setBounds(new Rectangle(136, 174, 81, 23));
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField2.setText("9");
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jButton1);
jTextField2.setBounds(new Rectangle(223, 96, 77, 20));
}
public static void main(String[] args) {
Frame1 f = new Frame1();
f.setBounds(200,100,400,300);
f.setVisible(true);
}
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
float a=Float.parseFloat(jTextField1.getText());
int b=Integer.parseInt(jTextField2.getText());
System.out.println(a*b);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
} 展开
结果正常应该是 123.3 啊,怎样才能得到123.3啊?
下边是我 的 代码:
package untitled1;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
public Frame1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jTextField1.setText("13.7");
jTextField1.setBounds(new Rectangle(51, 96, 77, 20));
jButton1.setBounds(new Rectangle(136, 174, 81, 23));
jButton1.setText("jButton1");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField2.setText("9");
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jButton1);
jTextField2.setBounds(new Rectangle(223, 96, 77, 20));
}
public static void main(String[] args) {
Frame1 f = new Frame1();
f.setBounds(200,100,400,300);
f.setVisible(true);
}
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
float a=Float.parseFloat(jTextField1.getText());
int b=Integer.parseInt(jTextField2.getText());
System.out.println(a*b);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询