admin管理员组文章数量:1531313
2024年7月23日发(作者:)
8.1 数据类型转化类
本节所列函数和过程一般都定义在 System 或者 SysUtils 单元。
8.1.1 数值和字符串的相互转化
procedure Str(X [: Width [: Decimals ]]; var S);
将数值 X 按照一定格式转化为字符串 S。Width 指定 S 的总长度,如果 X 是实数,Decimals 指定小数点后的位
数。如:
var
S: String;
begin
Str(12.2:6:2,S); {S=’ 12.20’}
end;
procedure Val(S; var V; var Code: Integer);
将字符串 S 转化为数值 V。如果不能转化,则 Code 返回第一个非法字符的位置。如:
var
V,Code: Integer;
begin
Val(’12.00’, V, Code); {没能成功转化,Code返回’.’的位置3}
end;
8.1.2 整数和字符串的相互转化
function IntToStr(Value: Integer): string; overload;
function IntToStr(Value: Int64): string; overload;
第一个用于 32Bit 整数的转化,第二个用于 64Bit 整数的转化。
因为 Cardinal、Longword 等和 Integer 是兼容的(即是说一个 Integer 变量总是可以无损失地存储
一个 Cardinal 或者 Longword 变量的值,因为 Integer 的取值范围完全包含了它们的取值范围)。
function StrToInt(const S: string): Integer;
function StrToInt64(const S: string): Int64;
将一个字符串转化为整数。如果 S 包含非数字字符(如“ 1A”、“ 1.2”),则运行时产生异常。但是
S 也可以是十六进制表示方法,如“ $A”,那么会返回十进制数 10。
function StrToIntDef(const S: string; Default: Integer): Integer;
function StrToInt64Def(const S: string; Default: Int64): Int64;
转化的时候可以指定默认值。如果 S 非法,那么它们返回 Default 指定的默认值,不会产生异常。
function TryStrToInt(const S: string; out Value: Integer): Boolean;
function TryStrToInt64(const S: string; out Value: Int64): Boolean;
转化得到的整数保存在输出参数 Value,如果不能转化,则函数返回 False。
function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;
将十进制整数转化为十六进制整数,结果用字符串表示。
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer;
将十六进制整数转化为二进制整数,结果用字符串表示,保存在 Buffer 中。
8.1.3 实数和字符串的相互转化
function FloatToStr(Value: Extended): string;
function CurrToStr(Value: Currency): string;
实数转化为字符串。
function FloatToText(Buffer: PChar; const Value; ValueType: TFloatValue;
Format: TFloatFormat; Precision, Digits: Integer): Integer;
function CurrToStrF(Value: Currency; Format: TFloatFormat;
Digits: Integer): string;
function FormatCurr(const Format: string; Value: Currency): string;
function FloatToStrF(Value: Extended; Format: TFloatFormat;
Precision, Digits: Integer): string;
function FormatFloat(const Format: string; Value: Extended): string;
转化时,可以指定格式。
function StrToFloat(const S: string): Extended;
function StrToCurr(const S: string): Currency;
字符串转化为实数。
类似的函数还有:StrToFloatDef、StrToCurrDef、TryStrToFloat、TryStrToCurr 等,大家顾其名即可思其义了。
8.1.4 实数子类型的相互转化
function FloatToCurr(const Value: Extended): Currency;
等等。
版权声明:本文标题:Delphi常用函数和过程 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1721732749a894004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论