如何使用Java进行格式化输出?代码示例

2021年3月23日14:35:12 发表评论 559 次浏览

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

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

这是所有方法中最简单的方法, 因为这与C语言中的printf相似。请注意, System.out.print()和System.out.println()带有单个参数, 但是printf()可以带有多个参数。

// A Java program to demonstrate working of printf() in Java
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 precison: 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);
   }
}

输出如下:

Printing simple integer: x = 100
Formatted with precison: PI = 3.14
Formatted to specific width: n = 5.2000
Formatted to right margin: n =         2324435.2500

System.out.format()与printf()等效, 也可以使用。

使用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 precison: 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));
   }
}

输出如下:

Without fraction part: num = 123
Formatted to Give precison: num = 123.46
appended zeroes to right: num = 123.456700
formatting Numeric part : num = 00123.46
your Formatted Dream Income : $23, 456.79

使用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);
   }
}

输出如下:

Formatted Date : 09-08-2018
Parsed Date : Sat Feb 18 00:00:00 UTC 1995

参考文献:

https://docs.oracle.com/

javase / tutorial / essential / io /

formatting.html

https://docs.oracle.com/

javase / tutorial / java / data /

numberformat.html

http://docs.oracle.com/javase/

6 / docs / api / java / text /

SimpleDateFormat.html

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: