β

String Formatting

Phoenixjiangnan's Blog 123 阅读
System.out.println(String.format("Int: %d", 100));
System.out.println(String.format("Int: %08d", 100)); // 00000100

System.out.println(String.format("Float: %f", 100.111)); // 100.111000
System.out.println(String.format("Float: %.2f", 100.111)); // 100.11

System.out.println(String.format("String: %s", "a string"));

Date today = new Date();
System.out.println(String.format("Date: %tD", today)); // Date: 04/06/15
System.out.println(String.format("Date mm/dd/yy: %tm/%td/%ty", today, today, today)); // Date mm/dd/yy: 04/06/15
System.out.println(String.format("Date: %tc", today)); // Date: Mon Apr 06 09:10:47 PDT 2015
System.out.println(String.format("Int: %d", 100));
System.out.println(String.format("Int: %08d", 100)); // 00000100

System.out.println(String.format("Float: %f", 100.111)); // 100.111000
System.out.println(String.format("Float: %.2f", 100.111)); // 100.11

System.out.println(String.format("String: %s", "a string"));

Date today = new Date();
System.out.println(String.format("Date: %tD", today)); // Date: 04/06/15
System.out.println(String.format("Date mm/dd/yy: %tm/%td/%ty", today, today, today)); // Date mm/dd/yy: 04/06/15
System.out.println(String.format("Date: %tc", today)); // Date: Mon Apr 06 09:10:47 PDT 2015
作者:Phoenixjiangnan's Blog
原文地址:String Formatting, 感谢原作者分享。

发表评论