首页 课程 师资 教程 报名

Java格式化输出

  • 2022-08-25 10:15:58
  • 852次 动力节点

有时在竞争性编程中,必须以给定的指定格式打印输出。大多数用户都熟悉 C 中的 printf 函数。让我们讨论如何在 Java 中格式化输出。

我们可以通过多种方式在 Java 中格式化输出。其中一些在下面给出。

使用 System.out.printf()

使用 DecimalFormat 类

使用 SimpleDateFormat 类(用于格式化日期)

1. 使用 System.out.printf() 格式化输出

这是所有方法中最简单的,因为它类似于 C 中的 printf。注意 System.out.print() 和 System.out.println() 采用单个参数,但 printf() 可能采用多个参数。

// Java program to demonstrate working of printf()
class JavaFormatter1 {
	public static void main(String args[])
	{
		int x = 100;
		System.out.printf(
			"Printing simple integer: x = %d
", x);
		// this will print it upto 2 decimal places
		System.out.printf(
			"Formatted with precision: PI = %.2f
",
			Math.PI);
		float n = 5.2f;
		// automatically appends zero to the rightmost part
		// of decimal
		System.out.printf(
			"Formatted to specific width: n = %.4f
", n);
		n = 2324435.3f;
		// here number is formatted from right margin and
		// occupies a width of 20 characters
		System.out.printf(
			"Formatted to right margin: n = %20.4f
", n);
	}
}

输出

打印简单整数:x = 100
精确格式化:PI = 3.14
格式化为特定宽度:n = 5.2000
格式化为右边距:n = 2324435.2500

2.使用DecimalFormat类格式化

DecimalFormat 用于格式化十进制数。

// Java program to demonstrate working of DecimalFormat
import java.text.DecimalFormat;
class JavaFormatter2 {
	public static void main(String args[])
	{
		double num = 123.4567;
		// prints only numeric part of a floating number
		DecimalFormat ft = new DecimalFormat("####");
		System.out.println("Without fraction part: num = "
						+ ft.format(num));
		// this will print it upto 2 decimal places
		ft = new DecimalFormat("#.##");
		System.out.println(
			"Formatted to Give precision: num = "
			+ ft.format(num));
		// automatically appends zero to the rightmost part
		// of decimal instead of #,we use digit 0
		ft = new DecimalFormat("#.000000");
		System.out.println(
			"appended zeroes to right: num = "
			+ ft.format(num));
		// automatically appends zero to the leftmost of
		// decimal number instead of #,we use digit 0
		ft = new DecimalFormat("00000.00");
		System.out.println(
			"formatting Numeric part : num = "
			+ ft.format(num));
		// formatting money in dollars
		double income = 23456.789;
		ft = new DecimalFormat("$###,###.##");
		System.out.println("your Formatted Dream Income : "
						+ ft.format(income));
	}
}

输出

没有小数部分:num = 123
格式化以提供精度:num = 123.46
向右附加零:num = 123.456700
格式化数字部分:num = 00123.46
您的格式化梦想收入:23,456.79 美元

3. 使用 SimpleDateFormat 类格式化日期和解析

此类存在于 java.text 包中。

// Java program to demonstrate working of SimpleDateFormat
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Formatter3 {
	public static void main(String args[])
		throws ParseException
	{
		// Formatting as per given pattern in the argument
		SimpleDateFormat ft
			= new SimpleDateFormat("dd-MM-yyyy");
		String str = ft.format(new Date());
		System.out.println("Formatted Date : " + str);
		// parsing a given String
		str = "02/18/1995";
		ft = new SimpleDateFormat("MM/dd/yyyy");
		Date date = ft.parse(str);
		// this will print the date as per parsed string
		System.out.println("Parsed Date : " + date);
	}
}

输出

格式化日期:24-01-2022
解析日期:1995 年 2 月 18 日星期六 00:00:00 UTC

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交