java里面怎么format number?double i,j;
要把i/j的结果format成含有2位小数的格式, 例如 3.03, 4.00, 120.32 之类的格式.
请问怎么做?
Thanks~
如果你是说CS1102 lab 0的话
import java.io.*;
import java.util.*;
import java.text.NumberFormat;
class Q4 {
//main class
public static void main(String []args) throws Exception{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String thisLine;
StringTokenizer st;
double a, b;
boolean blank = false;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
while((thisLine=stdin.readLine())!=null){
st = new StringTokenizer(thisLine);
a = Double.parseDouble(st.nextToken());
b = Double.parseDouble(st.nextToken());
if (blank)
System.out.print(" ");
else
blank = true;
System.out.print(nf.format(a/b));
}
System.out.println();
}
}
import java.util.*;
import java.text.NumberFormat;
class Q4 {
//main class
public static void main(String []args) throws Exception{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String thisLine;
StringTokenizer st;
double a, b;
boolean blank = false;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
while((thisLine=stdin.readLine())!=null){
st = new StringTokenizer(thisLine);
a = Double.parseDouble(st.nextToken());
b = Double.parseDouble(st.nextToken());
if (blank)
System.out.print(" ");
else
blank = true;
System.out.print(nf.format(a/b));
}
System.out.println();
}
}