我有一个用于计算数据范围的 GUI Java 代码,例如,如果输入了两个值2.444
,3.555
结果将是一个长双精度值1.11100000...
等。如何指定它应该显示的小数点后的位数?(例如%.2f
:)
这是我的代码:
public class Range
{
public static void main(String args[])
{
int num=0; //number of data
double d; //the data
double smallest = Integer.MAX_VALUE;
double largest = Integer.MIN_VALUE;
double range = 0;
String Num =
JOptionPane.showInputDialog("Enter the number of data ");
num=Integer.parseInt(Num);
for(int i=0; i<num; i++)
{
String D =
JOptionPane.showInputDialog("Enter the data ");
d=Double.parseDouble(D);
if(d < smallest)
smallest = d;
if(d > largest)
largest = d;
}
range = largest - smallest ; //calculating the range of the input
JOptionPane.showMessageDialog(null,"Range = "+smallest+"-"+largest+" = "+range,"Range",JOptionPane.PLAIN_MESSAGE);
}
}