Java 限制小数位数
我有一个基本问题。我不太擅长编程。我试图在我的答案字段中显示小数点后2位的答案。我有我的JTextFields和我的JRadioBtn集。当回答我的面积或周长时,有很多小数位。我只需要把小数点后两位四舍五入。以下是我目前掌握的代码:Java 限制小数位数,java,swing,jtextfield,number-formatting,decimal,Java,Swing,Jtextfield,Number Formatting,Decimal,我有一个基本问题。我不太擅长编程。我试图在我的答案字段中显示小数点后2位的答案。我有我的JTextFields和我的JRadioBtn集。当回答我的面积或周长时,有很多小数位。我只需要把小数点后两位四舍五入。以下是我目前掌握的代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import static java.lang.Math.*; public class Triangle extends JFra
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.lang.Math.*;
public class Triangle extends JFrame
{
private GridBagLayout layout = new GridBagLayout();
private GridBagConstraints constraints = new GridBagConstraints();
JTextField firstNumberTxt, secondNumberTxt, thirdNumberTxt, answerTxt;
JRadioButton addBtn, subBtn, multBtn, divBtn;
JButton calculateBtn, exitBtn;
public ExamQuestion2()
{
super( "A Simple Calculator Form." );
firstNumberTxt = new JTextField( 5 );
secondNumberTxt = new JTextField( 5 );
answerTxt = new JTextField( 5 );
addBtn = new JRadioButton( "area", true );
subBtn = new JRadioButton( "perimeter", false );
calculateBtn = new JButton( "cal" +
"calculate" );
exitBtn = new JButton( "exit" );
JLabel firstNumberLbl = new JLabel( "Base: "),
operationLbl = new JLabel( "Operation "),
secondNumberLbl = new JLabel( "Height: "),
answerLbl =new JLabel( "Answer: ");
setLayout( layout );
addComponent( firstNumberLbl, 0, 0, 1, 1, GridBagConstraints.EAST );
addComponent( firstNumberTxt, 0, 1, 1, 1, GridBagConstraints.WEST );
addComponent( operationLbl, 1, 0, 1, 4, GridBagConstraints.CENTER );
addComponent( addBtn, 1, 1, 1, 1, GridBagConstraints.WEST );
addComponent( subBtn, 2, 1, 1, 1, GridBagConstraints.WEST );
addComponent( secondNumberLbl, 5, 0, 1, 1, GridBagConstraints.EAST );
addComponent( secondNumberTxt, 5, 1, 1, 1, GridBagConstraints.WEST );
addComponent( answerLbl, 6, 0, 1, 1, GridBagConstraints.EAST );
addComponent( answerTxt, 6, 1, 1, 1, GridBagConstraints.WEST );
addComponent( calculateBtn, 7, 0, 1, 1, GridBagConstraints.EAST );
addComponent( exitBtn, 7, 1, 1, 1, GridBagConstraints.WEST );
calculateBtn.addActionListener( new Calculator() );
exitBtn.addActionListener( new Exiter() );
}
private void addComponent( Component component, int row, int column, int width, int height, int anch )
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
constraints.anchor = anch;
constraints.weighty = 5;
add( component );
layout.setConstraints( component, constraints );
}
private class Calculator implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String firstString, secondString;
int choice;
int firstNumber, secondNumber;
firstString = firstNumberTxt.getText();
secondString = secondNumberTxt.getText();
if( firstString.length() < 1 || secondString.length() < 1)
{
answerTxt.setText( " INVALID" );
return;
}
firstNumber = Integer.parseInt( firstString );
secondNumber = Integer.parseInt( secondString );
if( addBtn.isSelected() )
{
choice = 0;
subBtn.setSelected( false );
}
if( addBtn.isSelected() ) choice = 0;
else if( subBtn.isSelected() ) choice = 1;
else if( multBtn.isSelected() ) choice = 2;
else choice = 3;
switch ( choice )
{
case 0: answerTxt.setText( ( (firstNumber * secondNumber) / 2 ) + "" ); break;
case 1: answerTxt.setText( ( firstNumber + secondNumber + sqrt( (firstNumber * firstNumber) +
(secondNumber * secondNumber))) + "" ); break;
default: if ( secondNumber != 0 )
{
answerTxt.setText( ( firstNumber / secondNumber ) + "" ); break;
}
else answerTxt.setText( " INVALID " ); break;
}
}
}
public static void main(String[] args)
{
Triangle MyCalculator= new Triangle();
MyCalculator.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
MyCalculator.setSize( 300, 300 );
MyCalculator.setLocationRelativeTo(null);
MyCalculator.setVisible( true );
}
private class Exiter implements ActionListener
{
public void actionPerformed( ActionEvent event)
{
System.exit(0);
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入静态java.lang.Math.*;
公共类三角形扩展JFrame
{
私有GridBagLayout布局=新建GridBagLayout();
私有GridBagConstraints=新GridBagConstraints();
JTextField FirstNumberText、SecondNumberText、thirdNumberTxt、answerTxt;
JRadioButton addBtn、subBtn、multBtn、divBtn;
JButton calculateBtn,exitBtn;
公开考试2()
{
超级(“一个简单的计算器形式”);
FirstNumberText=新的JTextField(5);
SecondNumberText=新的JTextField(5);
answerTxt=新的JTextField(5);
addBtn=新的JRadioButton(“区域”,真);
subBtn=新的JRadioButton(“周长”,假);
calculateBtn=新的JButton(“cal”+
“计算”);
exitBtn=新的JButton(“退出”);
JLabel firstnumberbl=新的JLabel(“基:”),
operationLbl=新的JLabel(“操作”),
SecondNumberBL=新的JLabel(“高度:”),
answerLbl=新的JLabel(“答案:”);
设置布局(布局);
addComponent(firstNumberBL、0、0、1、1、GridBagConstraints.EAST);
addComponent(firstNumberText,0,1,1,1,GridBagConstraints.WEST);
添加组件(操作LBL、1、0、1、4、GridBagConstraints.CENTER);
addComponent(addBtn,1,1,1,1,gridbagstraints.WEST);
addComponent(subBtn,2,1,1,1,gridbagstraints.WEST);
addComponent(SecondNumberBL、5、0、1、1、GridBagConstraints.EAST);
addComponent(secondnumberText,5,1,1,1,gridbagstraints.WEST);
addComponent(answerLbl,6,0,1,1,gridbagstraints.EAST);
addComponent(answerTxt,6,1,1,1,gridbagstraints.WEST);
addComponent(calculateBtn,7,0,1,1,GridBagConstraints.EAST);
addComponent(exitBtn,7,1,1,1,GridBagConstraints.WEST);
calculateBtn.addActionListener(新计算器());
exitBtn.addActionListener(新的Exiter());
}
私有void addComponent(组件组件、int行、int列、int宽度、int高度、int锚点)
{
constraints.gridx=列;
constraints.gridy=行;
constraints.gridwidth=宽度;
constraints.gridheight=高度;
约束。锚=锚;
约束条件。权重=5;
添加(组件);
布局。设置约束(组件、约束);
}
私有类计算器实现ActionListener
{
已执行的公共无效操作(操作事件)
{
字符串第一个字符串,第二个字符串;
智力选择;
int firstNumber,secondNumber;
firstString=firstNumberText.getText();
secondString=secondnumberText.getText();
if(firstString.length()<1 | | secondString.length()<1)
{
answerTxt.setText(“无效”);
返回;
}
firstNumber=Integer.parseInt(firstString);
secondNumber=Integer.parseInt(secondString);
如果(addBtn.isSelected())
{
选择=0;
子BTN.setSelected(假);
}
如果(addBtn.isSelected())选项=0;
如果(subBtn.isSelected())选项=1,则为else;
如果(multBtn.isSelected())选择=2;
其他选择=3;
开关(选择)
{
案例0:answerTxt.setText(((第一个数字*第二个数字)/2)+“”),break;
案例1:answerTxt.setText((firstNumber+secondNumber+sqrt((firstNumber*firstNumber)+
(第二个数字*第二个数字))+“”);中断;
默认值:if(secondNumber!=0)
{
setText((firstNumber/secondNumber)+“”);break;
}
else answerTxt.setText(“无效”);中断;
}
}
}
公共静态void main(字符串[]args)
{
三角形MyCalculator=新三角形();
MyCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalculator.setSize(300300);
MyCalculator.setLocationRelativeTo(空);
MyCalculator.setVisible(true);
}
私有类Exiter实现ActionListener
{
已执行的公共无效操作(操作事件)
{
系统出口(0);
}
}
}
我在上网本上找到了一些东西,我已经看过了,也试过了,但似乎什么都做不好。有人需要帮助吗?尝试使用:
String.format("%.2f", numberToBeRounded);
例如,您可以将包含案例0
的行替换为以下内容:
case 0: answerTxt.setText( String.format("%.2f", (firstNumber * secondNumber) / 2 ) ); break;
%.2f
格式说明符将显示四舍五入到小数点后两位的浮点数。有关格式化字符串的详细信息,请参阅文档。尝试NumberFormat.getCurrencyInstance()
;这里有一个相关的例子。除了显示数字格式的NumberFormat
之外,我建议您从代码中退一步,根据您的数字计算需要查看该类。您可以设置刻度BigDecimal(biginger unscaledVal,int scale)
并有效(正确读取)执行数值计算。尝试NumberFormat.getCurrencyInstance()
;有一个相关的例子,你给我的例子给了我答案。谢谢你的帮助。你是最棒的!!!!我认为“g”和“g”的转换使用精度作为四舍五入后所得幅值的总位数。您可能需要“e”、“e”或“f”,其精度为小数点分隔符后的位数。请说明为什么这样做。您的答案有被删除的危险,因为它显示为一篇低质量的文章。@deezy:您可以引用上的格式化程序语法部分进一步扩展您的答案。