System.out.format可以格式日期,数字。。

Java格式化输出示例
本文介绍了Java中使用System.out.format进行数据格式化的多种方法,包括整数、浮点数和日期时间的格式化技巧,并展示了如何利用DecimalFormat类自定义数值格式。
import java.util.Calendar;
import java.util.Locale;
public class TestFormat {
    
    public static void main(String[] args) {
      long n = 461012;
      System.out.format("%d%n", n);                  //  -->  "461012"
      System.out.format("%08d%n", n);                //  -->  "00461012"
      System.out.format("%+8d%n", n);                //  -->  " +461012"
      System.out.format("%,8d%n", n);                //  -->  " 461,012"
      System.out.format("%+,8d%n%n", n);             //  -->  "+461,012"
      
      double pi = Math.PI;
      System.out.format("%f%n", pi);                 //  -->  "3.141593"
      System.out.format("%.3f%n", pi);               //  -->  "3.142"
      System.out.format("%10.3f%n", pi);             //  -->     3.142"
      System.out.format("%-10.3f%n", pi);            //  -->  "3.142"
      System.out.format(Locale.FRANCE,
                        "%-10.4f%n%n", pi);          //  -->  "3,1416"
      Calendar c = Calendar.getInstance();
      System.out.format("%tB %te, %tY%n", c, c, c);  //  -->  "May 29, 2006"
      System.out.format("%tl:%tM %tp%n", c, c, c);   //  -->  "2:34 am"
      System.out.format("%tD%n", c);                 //  -->  "05/29/06"
    }
}
  
  
====================
import java.text.*;
public class DecimalFormatDemo {
   static public void customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      String output = myFormatter.format(value);
      System.out.println(value + "  " + pattern + "  " + output);
   }

 

   static public void main(String[] args) {

      customFormat("###,###.###", 123456.789);
      customFormat("###.##", 123456.789);
      customFormat("000000.000", 123.78);
      customFormat("$###,###.###", 12345.67); 
   }
}

 


The output is:

 

123456.789  ###,###.###  123,456.789
123456.789  ###.##  123456.79
123.78  000000.000  000123.780
12345.67  $###,###.###  $12,345.67

Converters and Flags Used in TestFormat.java(只是例子中用到的,还有没有用到的
ConverterFlagExplanation
d A decimal integer.
f A float.
n A new line character appropriate to the platform running the application. You should always use %n, rather than \n.
tB A date & time conversion—locale-specific full name of month.
td, te A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not.
ty, tY A date & time conversion—ty = 2-digit year, tY = 4-digit year.
tl A date & time conversion—hour in 12-hour clock.
tM A date & time conversion—minutes in 2 digits, with leading zeroes as necessary.
tp A date & time conversion—locale-specific am/pm (lower case).
tm A date & time conversion—months in 2 digits, with leading zeroes as necessary.
tD A date & time conversion—date as %tm%td%ty
 08Eight characters in width, with leading zeroes as necessary.
 +Includes sign, whether positive or negative.
 ,Includes locale-specific grouping characters.
 -Left-justified..
 .3Three places after decimal point.
 10.3Ten characters in width, right justified, with three places after decimal point.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值