java保留两位小数

Python08

java保留两位小数,第1张

public class Main {

public static void main(String[] args) {

float  f1 = 1234.5678f 

int    f2 = 99900 

String f3 = String.format("x=%.2f, y=%.3f", 100.22222f, 200.33333f)

String f4 = String.format("x=%.4f, y=%.5f", 100.22222f, 200.33333f)

System.out.printf("f1 = %.2f\n", f1)

System.out.printf("f2 = %.2f\n", (float)f2)

System.out.printf("f3 = \"%s\"\n", f3)

System.out.printf("f4 = \"%s\"\n", f4)

}

}

输出:

f1 = 1234.57

f2 = 99900.00

f3 = "x=100.22, y=200.333"

f4 = "x=100.2222, y=200.33333"

按照你的要求编写的Java程序如下:

import java.util.Scanner

public class A {

 public static void main(String[] args) {

  int b=1024

  Scanner sc=new Scanner(System.in)

  System.out.println("请输入任意整数:")

  int a=sc.nextInt()

  double c=(double)a/b

  System.out.println(String.format("%.1f", c))

 }

}

运行结果:

请输入任意整数:

3679

3.6