大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java格式化輸出

Java格式化輸出

更新時間:2022-08-25 10:15:58 來源:動力節(jié)點 瀏覽1399次

有時在競爭性編程中,必須以給定的指定格式打印輸出。大多數(shù)用戶都熟悉 C 中的 printf 函數(shù)。讓我們討論如何在 Java 中格式化輸出。

我們可以通過多種方式在 Java 中格式化輸出。其中一些在下面給出。

使用 System.out.printf()

使用 DecimalFormat 類

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

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

這是所有方法中最簡單的,因為它類似于 C 中的 printf。注意 System.out.print() 和 System.out.println() 采用單個參數(shù),但 printf() 可能采用多個參數(shù)。

// 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\n", x);
		// this will print it upto 2 decimal places
		System.out.printf(
			"Formatted with precision: PI = %.2f\n",
			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);
		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", n);
	}
}

輸出

打印簡單整數(shù):x = 100
精確格式化:PI = 3.14
格式化為特定寬度:n = 5.2000
格式化為右邊距:n = 2324435.2500

2.使用DecimalFormat類格式化

DecimalFormat 用于格式化十進制數(shù)。

// 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));
	}
}

輸出

沒有小數(shù)部分:num = 123
格式化以提供精度:num = 123.46
向右附加零:num = 123.456700
格式化數(shù)字部分: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

 

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 国产大战女模特在线视频 | 人人爽天天碰天天躁夜夜躁 | 精品国产一区二区三区在线观看 | 日韩欧美综合 | 国产亚洲精品日韩综合网 | 日韩女人毛片在线播放 | 久久好色| 亚洲精品成人456在线播放 | 精品人人| 无毒不卡| 国产久视频观看 | 亚洲日本中文字幕在线 | 日韩精品成人免费观看 | 久久久久久久尹人综合网亚洲 | 很黄很色的小视频在线网站 | 国产黄色自拍视频 | 玖玖射 | 日本人一级毛片视频 | 成人国产精品一区二区网站 | 国产影片中文字幕 | 狠狠色噜噜狠狠狠8888米奇 | 国产免费播放 | 国产人成激情视频在线观看 | 欧美一级毛片在线一看 | 成年人视频在线免费 | 日韩免费视频观看 | 日韩高清不卡在线 | 在线播放性xxx欧美 在线播放亚洲 | 一级大黄视频 | 久久久噜噜噜www成人网 | 麻豆国产原创 | 日本高清h色视频在线观看 日本高清不卡二区 | 久99频这里只精品23热 视频 | 五月婷中文字幕 | www国产视频 | 国内免费在线视频 | 黄片一级毛片 | 中文字幕日韩国产 | 欧美性白人顶级hd | 免费看成人播放毛片 | 亚洲成人国产 |