我有一个用于计算数据范围的 GUI Java 代码,例如,如果输入两个值 2.444
和 3.555
,结果将是一个 long double 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);
}
}
最佳答案
关于Java:指定小数点后有多少个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638331/