Delphi 格式化函数Format、FormatDateTime与FormatFloat详解
- Format函数
- %d 代表一个整数
- %u 代表一个无负号整数
- %s 代表字符串
- %f 代表浮点数(保留或凑够两位小数点 )
- %g 代表浮点数(会去掉多余的 0)
- %n 代表浮点数(整数部分使用千位分隔符, 保留两位小数点)
- %m 代表浮点数(加货币记号, 转换结果取决于系统的地域设置)
- %e 代表用科学计数法表示整数或浮点数
- %p 代表指针地址(用十六进制表示)
- %x 用十六进制表示一个整数
- %0:s 用序号来指定替换顺序
- %1:s%0:s%0:s%1:s 反复使用 序号
- %d,%8d,%d 指定了8个字符的宽度, 少了就用空格凑足8个字符, 多了则无效果。
- %d,%-8d,%d - 表示左对齐, 默认是右对齐的
- %.9f 指定小数点的位数, 取值范围1-9, 输入0也当1用
- %.5d 这是给整数指定位数, 如果小了则无效
- %.3s 给字符串指定位数, 如果超过了源字符串长度则无效
- %.3e 给科学计数法指定位数
- "%" [index ":"] ["-"] [width] ["." prec] type 指令顺序
- FormatDateTime
- c 表示用短格式显示日期与时间
- ddddd 五个 d 表示短格式日期
- dddddd 六个 d 表示长格式日期
- e 表示年, 1位
- ee 表示年, 2位
- eee 与 eeee 表示年, 返回4位数年
- m 表示月, 1位
- mm 表示月, 2位
- mmm 与 mmmm 表示长格式月
- d 表示日
- dd 表示双位日
- ddd 与 dddd 表示星期; 可能对不同的语种会有区别
- h hh 表示时
- n nn 表示分
- s ss 表示秒
- z zz zzz 表示毫秒
- y yy yyy yyyy 表示年; 和 e 略有不同
- t tt 表示时间
- ampm 表示上午、下午
- 使用分隔符, - 是默认的, / 是与 - 等效的, 假如我非要用 / 显示呢?
- 混入的字符串要包含在双引号中
- FormatFloat
Format函数
根据指定所需要的格式,格式化字符串。
原型: function Format(const Format: string const Args: array of const): string;
按我的理解这其实就是一个替换函数,把第二个参数[]中的数据依次替换第一个参数中的%x,
而我们只要记住%x的具体含义就能玩转这个函数。
%d 代表一个整数
//返回: 最大整数是: 2147483647; 最小整数是: -2147483648
s := Format('最大整数是: %d; 最小整数是: %d', [High(Integer), Low(Integer)]);
%u 代表一个无负号整数
//返回: 最大的无符号整数是: 4294967295
s := Format('最大的无负号整数是: %u', [High(Cardinal)]);
%s 代表字符串
//返回: 你好! 我是万一
s := Format('%s! %s', ['你好', '我是万一']);
%f 代表浮点数(保留或凑够两位小数点 )
//返回: Pi的值是: 3.14
s := Format('%f', [Pi]);
%g 代表浮点数(会去掉多余的 0)
//返回: 1.2345
s := Format('%g',[01.2345000]);
%n 代表浮点数(整数部分使用千位分隔符, 保留两位小数点)
//返回: 12,345.68
s := Format('%n',[12345.6789]);
%m 代表浮点数(加货币记号, 转换结果取决于系统的地域设置)
//返回: ¥12,345.68
s := Format('%m',[12345.6789]);
%e 代表用科学计数法表示整数或浮点数
//返回: 1.23456789000000E+004
s := Format('%e',[12345.6789]);
%p 代表指针地址(用十六进制表示)
//返回: 0012F5BC
s := Format('%p',[@Self]);
%x 用十六进制表示一个整数
//返回: FF
s := Format('%x',[255]);
如果想要指定输出一个8位十六进制(比如内存地址),可以使用%.8x
%0:s 用序号来指定替换顺序
//返回: 一万
s := Format('%1:s%0:s',['万','一']);
%1:s%0:s%0:s%1:s 反复使用 序号
//返回:一万万一
s := Format('%1:s%0:s%0:s%1:s',['万','一']);
%d,%8d,%d 指定了8个字符的宽度, 少了就用空格凑足8个字符, 多了则无效果。
//返回: 1, 2,3
s := Format('%d,%8d,%d',[1,2,3]);
%d,%-8d,%d - 表示左对齐, 默认是右对齐的
//返回: 1,2 ,3
s := Format('%d,%-8d,%d',[1,2,3]);
%.9f 指定小数点的位数, 取值范围1-9, 输入0也当1用
//返回: 3.141592654
s := Format('%.9f',[Pi]);
%.5d 这是给整数指定位数, 如果小了则无效
//返回: 00012
s := Format('%.5d',[12]);
%.3s 给字符串指定位数, 如果超过了源字符串长度则无效
//返回: For
s := Format('%.3s',['Format']);
%.3e 给科学计数法指定位数
//返回: 1.23E+002
s := Format('%.3e',[123.456]);
"%" [index ":"] ["-"] [width] ["." prec] type 指令顺序
FormatDateTime
原型:function FormatDateTime(const Format: string DateTime: TDateTime): string;
FormatDateTime 的参数1是 String 格式指令, 参数2是 TDateTime 类型的时间
c 表示用短格式显示日期与时间
s := FormatDateTime('c', Now); {返回: 2007-12-18 23:56:05}
ddddd 五个 d 表示短格式日期
s := FormatDateTime('ddddd', Now); {返回: 2007-12-19}
dddddd 六个 d 表示长格式日期
s := FormatDateTime('dddddd', Now); {返回: 2007年12月19日}
e 表示年, 1位
s := FormatDateTime('e', Now); {返回: 7}
ee 表示年, 2位
s := FormatDateTime('ee', Now); {返回: 07}
eee 与 eeee 表示年, 返回4位数年
s := FormatDateTime('eee', Now); {返回: 2007}
s := FormatDateTime('eeee', Now); {返回: 2007}
m 表示月, 1位
s := FormatDateTime('m', Now); {返回: 12}
mm 表示月, 2位
s := FormatDateTime('mm', StrToDateTime('2008-1-1')); {返回: 01}
mmm 与 mmmm 表示长格式月
s := FormatDateTime('mmm', Now); {返回: 十二月}
s := FormatDateTime('mmmm', Now); {返回: 十二月}
d 表示日
s := FormatDateTime('d', Now); {返回: 19}
s := FormatDateTime('d', StrToDateTime('2008-1-1')); {返回: 1}
dd 表示双位日
s := FormatDateTime('dd', Now); {返回: 19}
s := FormatDateTime('dd', StrToDateTime('2008-1-1')); {返回: 01}
ddd 与 dddd 表示星期; 可能对不同的语种会有区别
s := FormatDateTime('ddd', Now); {返回: 周六}
s := FormatDateTime('dddd', Now); {返回: 星期六}
h hh 表示时
s := FormatDateTime('h', StrToDateTime('2007-12-30 9:58:06')); {返回: 9}
s := FormatDateTime('hh', StrToDateTime('2007-12-30 9:58:06')); {返回: 09}
n nn 表示分
s := FormatDateTime('n', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s := FormatDateTime('nn', StrToDateTime('2007-12-30 9:58:06')); {返回: 58}
s ss 表示秒
s := FormatDateTime('s', StrToDateTime('2007-12-30 9:58:06')); {返回: 6}
s := FormatDateTime('ss', StrToDateTime('2007-12-30 9:58:06')); {返回: 06}
z zz zzz 表示毫秒
s := FormatDateTime('z', Now); {返回: 24}
s := FormatDateTime('zz', Now); {返回: 524}
s := FormatDateTime('zzz', Now); {返回: 524}
y yy yyy yyyy 表示年; 和 e 略有不同
s := FormatDateTime('y', Now); {返回: 07}
s := FormatDateTime('yy', Now); {返回: 07}
s := FormatDateTime('yyy', Now); {返回: 2007}
s := FormatDateTime('yyyy', Now); {返回: 2007}
t tt 表示时间
s := FormatDateTime('t', Now); {返回: 0:21}
s := FormatDateTime('tt', Now); {返回: 0:22:13}
ampm 表示上午、下午
s := FormatDateTime('ampm', Now); {返回: 上午}
s := FormatDateTime('tampm', Now); {返回: 0:24 上午}
使用分隔符, - 是默认的, / 是与 - 等效的, 假如我非要用 / 显示呢?
s := FormatDateTime('yy/mm/dd', Now); {返回: 07/12/19}
s := FormatDateTime('yy/mm/dd', Now); {返回: 07-12-19}
s := FormatDateTime('yy-mm-dd', Now); {返回: 07-12-19}
s := FormatDateTime('yymmdd', Now); {返回: 071219}
s := FormatDateTime('yy"/"mm"/"dd', Now); {返回: 07/12/19}
混入的字符串要包含在双引号中
s := FormatDateTime('"当前时间是: "yyyy-m-d h:n:s:zz', Now); {返回: 当前时间是: 2007-12-19 0:47:16:576}
FormatFloat
格式化浮点数
原型:function FormatFloat(const Format: string Value: Extended): string;
FormatFloat 的参数1是 String 格式指令, 参数2是实数类型 Extended
//返回: 12.346
s := FormatFloat('###.###',12.3456);
//返回: 012.346
s := FormatFloat('000.000',12.3456);
//返回: 12.3
s := FormatFloat('#.###',12.3);
//返回: 12.300
s := FormatFloat('0.000',12.3);
//返回: 1,234,567
s := FormatFloat('#,#.#',1234567);
//返回: 1,234,567.0
s := FormatFloat('0,0.0',1234567);
//返回: 1.23E+6
s := FormatFloat('0.00E+0',1234567);
//返回: 1.23E+06
s := FormatFloat('0.00E+00',1234567);
//在科学计数法中使用 # 好像不合适?
参看资料:
https://www.cnblogs.com/jijm123/p/11120147.html
http://www.cnblogs.com/del/archive/2007/12/18/1005161.html
http://www.cnblogs.com/del/archive/2007/12/19/1005205.html
http://www.cnblogs.com/del/archive/2007/12/19/1005212.html
http://www.cnblogs.com/sonicit/archive/2007/06/04/769882.html
Delphi 格式化函数Format、FormatDateTime与FormatFloat详解的更多相关文章
- Delphi格式化函数Format、FormatDateTime和FormatFloat详解
转自:http://outofmemory.cn/code-snippet/7631/Delphi-format-hua-function-Format-FormatDateTime-FormatFl ...
- Delphi 格式化函数 Format函数
function Format(const Format: string; const Args: array of const): string; function Format(const For ...
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- 教程-Delphi中Spcomm使用属性及用法详解
Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...
- Delphi下利用WinIo模拟鼠标键盘详解 有参考价值
https://blog.csdn.net/fgrass_163/article/details/6365296 Delphi下利用WinIo模拟鼠标键盘详解 2011年04月26日 21:03:00 ...
- (转载)--SG函数和SG定理【详解】
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
- 转:MySQL Row Format(MySQL行格式详解)
MySQL Row Format(MySQL行格式详解) --转载自登博的博客
- SQL Server中排名函数row_number,rank,dense_rank,ntile详解
SQL Server中排名函数row_number,rank,dense_rank,ntile详解 从SQL SERVER2005开始,SQL SERVER新增了四个排名函数,分别如下:1.row_n ...
- Delphi - Windows自动计划任务与ParamStr详解
Windows自动计划任务与ParamStr详解 ParamStr函数: ParamStr(1),..ParamStr(N) ParamStr(1)代表程序入口的第一个参数,同理,ParamStr(N ...
- php strpos() 函数介绍与使用方法详解
本文主要和大家介绍PHP中mb_strpos的使用技巧,通过使用语法以及实例给大家详细分析了用法,需要的朋友参考学习下.希望能帮助到大家.mb_strpos(PHP 4 >= 4.0.6, PH ...
随机推荐
- webpack核心用法,为什么要使用webpack
一:为什么使用webpack 1. 代码转换.文件优化.代码分割.模块合并.自动刷新.等等 2. webpack上手 <!DOCTYPE html> <html lang=" ...
- JZOJ 4872.集体照
\(\text{Problem}\) 一年一度的高考结束了,我校要拍集体照.本届毕业生共分 \(n\) 个班,每个班的人数为 \(A_i\).这次拍集体照的要求非常奇怪:所有学生站一排,且相邻两个学生 ...
- 用ChatGPT,绘制一个账号系统的C4架构图
hi,我是熵减,见字如面. 昨天我们用ChatGPT来设计一个账号系统,并尝试输出:模型表,类关系图,序列图,状态图等常用的架构设计中常用的元素. 今天,我们继续向更高层级延伸一下,看ChatGPT能 ...
- ASAS-CoMoSpA研究: 评价SpA不同分类标准的表现
ASAS-CoMoSpA研究: 评价SpA不同分类标准的表现 EULAR2015; PresentID: OP0037 THE PERFORMANCE OF DIFFERENT CLASSIFICAT ...
- Linux(CentOS)安装Redis保姆级教程
Linux(CentOs)安装Redis教程 一,下载Redis(两种方式) 1,找到redis官网(https://redis.io/download) 如果想下载指定版本就去这个网址(https: ...
- day08-MyBatis的关联映射02
MyBatis的关联映射02 3.一对多 3.1基本介绍 mybatis – MyBatis 3 | XML 映射器 多对一关系也是一个基本的映射关系,多对一,也可以理解为一对多.例如: User-- ...
- C#的闭包捕获变量与英语中Nice to meet you的联系
看标题有种"意大利面与42号混凝土"放在一起说的感觉,实际上,就是. 闭包捕获变量 我们都知道在C#里,闭包捕获的是变量,而不是变量值本身 每个Task在运行的时候,发现i的值是3 ...
- python 循环与判断
import random #导入模块import stringcount = 1 #计数器while count < 4: #循环(while : 当) for i in range(1): ...
- html音乐播放器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Redis系列目录
第一章 Redis系列之-redis初识 第二章 Redis系列之-常用命令及API的使用 第三章 Redis系列之-高级用法 第四章 Redis系列之-持久化 第五章 Redis系列之-使用常见问题 ...