import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class T {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 第一种
DecimalFormat df = new DecimalFormat("#0.00");
float data;
data = sc.nextFloat();
System.out.println(df.format(data));

// 第二种,模仿c语言的输出方式
System.out.printf("%.2f\n", data);

// 第三种
BigDecimal bg = new BigDecimal(data);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);

// 第四种
System.out.println(String.format("%.2f", data));

// 第五种
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(data));
}

}

相关文章:

  • 2021-11-23
  • 2021-09-15
  • 2022-12-23
  • 2021-12-18
  • 2022-01-11
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-09-07
  • 2022-12-23
  • 2021-04-28
相关资源
相似解决方案