1. 程式人生 > >Delphi中str函式用法

Delphi中str函式用法

delphi中F1裡講的很明白了
Delphi syntax:
procedure Str(X [: Width [: Decimals ]]; var S);
Description
In Delphi code, Str converts X to a string representation according to the Width and Decimals formatting parameters. The effect is like a call to Write except the resulting string is stored in S instead of being written to a text file.
X is an integer-type or real-type expression. Width and Decimals are integer-type expressions. S is a string-type variable or a zero-based character array variable if extended syntax is enabled.
F1裡的例子
function MakeItAString(I: Longint): string;
{ Convert any integer type to a string }
var
  S: string[11];
begin
  Str(I, S);
  Result:= S;
end;
begin
  Canvas.TextOut(10, 10, MakeItAString(-5322));
end;
懶的翻譯了,直接給你擺了一個解釋:
Str是將實數或整數型別的值轉換為相應的字串,比如Str(7,S),那麼,S中就是 '7 ',而對於實數,可以通過Str來進行格式化,比如你的Str(S3,8,2)就是說將S3的小數部分保留2位,多餘的部分
四捨五入
,總的數字個數為8不包擴小數點,多餘的部分在生成的字元前補從空格,比如: Str(165.345:4:2,S),則S中為 '165.35 ' Str(165.345:5:1,S),則S中為 ' 165.3 ',你可以看出小數部分只有一位,而前面補充了一個空格。
轉自:http://zhidao.baidu.com/link?url=MxmBEPodOzDA1eXIRRyHzMWP1HztLT0guQcCUmKanstypInRjLqcStiG8U6tDcsh0VLJZw0o-K0kv8bf3Y47C_