//例如这个例子米其结果为1.6666666.我想把小数点控制在后2为该如何编写代码?
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float a = 5;
float b = 3;
float c= a/b;
System.out.print("c的值为" +c);
}
}
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float a = 5;
float b = 3;
float c= a/b;
System.out.print("c的值为" +c);
}
}
import java.util.Scanner;
public class TestTextField {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float a = 5;
float b = 3;
float c= a/b;
float d = Math.round(c*100);
System.out.print("c的值为" +d/100);
}
}
System.out.print(df.format(1.66666));
DecimalFormat format = new DecimalFormat( "#.###");
String str = format.format(a);
System.out.println(str);
这个你想保留几位小数都可以的