Java 如何将数据值设置为小数点后三位?
这个应用程序快结束了,但是我需要输出数据值在输出文件的小数点后三位。到目前为止,最终输出是我需要的(资产文件中数据值的平方根)。以下是我为应用程序编写的代码:Java 如何将数据值设置为小数点后三位?,java,android,eclipse,Java,Android,Eclipse,这个应用程序快结束了,但是我需要输出数据值在输出文件的小数点后三位。到目前为止,最终输出是我需要的(资产文件中数据值的平方根)。以下是我为应用程序编写的代码: public void srAndSave(View view) { EditText edt1; EditText edt2; TextView tv; String infilename; String outfilename; tv = (TextView) findViewBy
public void srAndSave(View view)
{
EditText edt1;
EditText edt2;
TextView tv;
String infilename;
String outfilename;
tv = (TextView) findViewById(R.id.text_status);
//Get the name of the input file and output file
edt1 = (EditText) findViewById(R.id.edit_infile);
edt2 = (EditText) findViewById(R.id.edit_outfile);
infilename = edt1.getText().toString();
outfilename = edt2.getText().toString();
//Create an array that stores double values (up to 20)
double double_nums[] = new double[20];
int n = 0;//For storing the number of data values in the array
//Open the data file from the asset directory
//and make sure the data file exists
AssetManager assetManager = getAssets();
try
{
Scanner fsc = new Scanner(assetManager.open(infilename));
//Get the data values from the file
//and store them in the array double_nums
n = 0;
while(fsc.hasNext()){
double_nums[n] = fsc.nextDouble();
n++;
}
//Calls on square_root_it method
square_root_it(double_nums, n);
//Display that the file has been opened
tv.setText("Opening the input file and reading the file were "
+ " successful.");
fsc.close();
}
catch(IOException e)
{
tv.setText("Error: File " + infilename + " does not exist");
}
//Write the data to the output file and
//also make sure that the existence of the file
File outfile = new File(getExternalFilesDir(null), outfilename);
try
{
FileWriter fw = new FileWriter(outfile);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
int x;
for(x=0;x < n;x++)
pw.println(double_nums[x]); //Write the data values that are stored in
//the array, double_nums after the method call
//for square_root_it
pw.close();
}
catch(IOException e)
{
System.out.println("Error! Output file does already exist! You will overwrite"
+ " this file!");
}
} //end srAndSave
public static void square_root_it(double[] a, int num_items)
{
int i;
for(i=0; i < num_items; i++)
a[i] = Math.sqrt(a[i]); //Initialize the array a[i] to have the square root of
//the double values stored in double_nums[] array, and
//then return a[i]
} //end square_root_it
public void srAndSave(视图)
{
编辑文本edt1;
编辑文本edt2;
文本视图电视;
字符串填充名;
字符串输出名;
tv=(文本视图)findViewById(R.id.text\u状态);
//获取输入文件和输出文件的名称
edt1=(EditText)findViewById(R.id.edit\u infle);
edt2=(EditText)findViewById(R.id.edit_outfile);
infilename=edt1.getText().toString();
outfilename=edt2.getText().toString();
//创建一个存储双值(最多20个)的数组
双双精度[]=新双精度[20];
int n=0;//用于存储数组中的数据值数量
//从资产目录中打开数据文件
//并确保数据文件存在
AssetManager AssetManager=getAssets();
尝试
{
Scanner fsc=新扫描仪(assetManager.open(infilename));
//从文件中获取数据值
//并将它们存储在数组double_nums中
n=0;
while(fsc.hasNext()){
double_nums[n]=fsc.nextDouble();
n++;
}
//调用平方根方法
平方根(双数,n);
//显示文件已打开
tv.setText(“打开输入文件并读取文件是错误的”
+“成功。”);
fsc.close();
}
捕获(IOE异常)
{
tv.setText(“错误:文件“+infilename+”不存在”);
}
//将数据写入输出文件并
//还要确保文件的存在
File outfile=新文件(getExternalFilesDir(null),outfilename);
尝试
{
FileWriter fw=新的FileWriter(输出文件);
BufferedWriter bw=新的BufferedWriter(fw);
PrintWriter pw=新的PrintWriter(bw);
int x;
对于(x=0;x
您可以设置双精度格式,指定小数位数,如下所示:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.###");
System.out.print(df.format(d));
可能重复的