たとえば、2 つの値が入力2.444
され3.555
、結果が long double になる場合など、データの範囲を計算するための GUI Java コードが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);
}
}