java string format

来源:互联网 发布:mac能玩iphone手游 编辑:程序博客网 时间:2024/06/04 20:36

java string fromat 基本用法示例:

import java.util.Date;/*** *  * @author Administrator *    %[argument number] [flags] [width]  [.precision] type *eg: %         1$          ,       6            .1      f */  public class TestFormat {public static void main(String[] args){String s = String.format("%, d", 1000000000);s = String.format("I have %.2f bugs to fix.", 429342.3483);s = String.format("%1$,6.1f", 42.000);s = String.format("%d" , 42);  // 格式化10进制s = String.format("%.3f", 42.0000); //格式化浮点型s = String.format("%x", 42);   // 格式化16进制s = String.format("%c", 42);   // 字符型格式化int one = 2014;double two = 2014.1018;s = String.format("The bank is %,d out of %,.2f", one,two); //按顺序替换//格式化时间s = String.format("%tc", new Date());  // date and time s = String.format("%tr", new Date());  // just the timeDate today = new Date();s = String.format("%tA, %tB %td", today,today,today);  // week month and day.s = String.format("%tA, %<tB %<td", today);  //和上面一样的效果,去除重复的todaySystem.out.println(s);}}


0 0