字符串格式化-F.format

F.format方法用于格式化字符串。原型如下:

F.format(format[,arg1,...[,argn]]);

通过将arg1,...,argn的值根据一定的格式插入到format中,生成一个新的字符串。

基本用法

F.format("I'm {0}, and {1} years old.", "MAE", 23);
//I'm MAE, and 23 years old.

参数值中有日期

对于日期值,会自动调用F.formatdate来处理

F.format("Today is {0}.", new Date());
F.format("Today is {0:yyyy-MM-dd}.", new Date());

参数值中有键值对对象

format中直接通过键值名引用。

F.format("I'm {0.name}, and {0.age} years old.", {name : "MAE", age : 23});
//I'm MAE, and 23 years old.

格式化数字

F.format("The number is {0:E}.", 23.2353);//The number is 2.32353e+1.
F.format("The number is {0:E2}.", 23.2353);//The number is 2.32e+1.
F.format("The number is {0:F}.", 23.2353); //The number is 23.
F.format("The number is {0:F2} .", 23.2353);//The number is 23.24.
F.format("The number is '{0:X}'.", 234570234);//The number is 'DFB41FA'.
F.format("The number is '{0:X8}'.", 234570234);//The number is '0DFB41FA'.
F.format("The number is '{0:8.X}'.", 234570234);//The number is '0DFB41FA'.
F.format("The number is '{0:D8}'.", 2345702);//The number is '02345702'.

'Dx'用于截取或补足指定位数的数字,截取时,从右开始。
'n.x'用于对结果补足位数,用0补。

发表评论评论(0)