黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

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

Java格式化輸出

更新時間:2022-08-25 10:15:58 來源:動力節點 瀏覽1361次

有時在競爭性編程中,必須以給定的指定格式打印輸出。大多數用戶都熟悉 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\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);
	}
}

輸出

打印簡單整數: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

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 成人国产在线不卡视频 | 在线天堂中文字幕 | 国产女同一区二区在线 | 国产不卡一区二区视频免费 | 日韩色网站 | 国产l精品国产亚洲区久久 国产papa | 亚洲高清国产品国语在线观看 | 久久99国产视频 | 在线亚洲精品防屏蔽 | 天天拍夜夜添久久精品免费 | 青草免费视频 | 色噜噜狠狠色综合久 | 手机看片1024国产 | 欧美性生交xxxxx久久久 | 亚洲黄v | 在线视频免费观看a毛片 | 最近中文字幕免费国语 | 日韩a级片在线观看 | 97干婷婷 | 亚洲一区在线观看视频 | 国产精品成人网 | 国产精品一区高清在线观看 | 91亚洲国产成人久久精品网站 | 精品人人 | 欧美视频一区二区三区 | 国产亚洲一区二区三区在线 | 天天舔天天操天天干 | 高h猛烈做哭bl壮汉受欧美 | 丝袜美女网站 | 手机看片1204国内基地在线 | 欧美videosex极品hd | 韩日视频在线观看 | 女攻男受高h全文肉肉 | 色综合久久精品中文字幕 | 欧美太黄太色视频在线观看 | 国产精品欧美一区二区三区不卡 | 日韩影院在线观看 | 色黄三大片免费 | 免费看黄色片的网站 | 日韩大片观看网址 | 18未满禁止观看黄瓜视频 |