我有一个代码,其中有一个 Single datatype 变量,但我想在消息框中显示。例如
VAR data:Single;
data:=0;
data:=5633.67+1290.965;
Msgbox('The sum of Fractional number is-:'+IntTostr(data),mbinformation,MB_OK);
Run Code Online (Sandbox Code Playgroud)
我现在至少能想到两个选择。第一个是FloatToStr
函数,它没有文档记录,或者是使用函数的官方方式Format
,它为您指定所需的格式提供了更好的灵活性。这是一个FloatToStr
函数示例:
var
S: string;
Value: Single;
begin
Value := 1.2345;
S := FloatToStr(Value);
MsgBox('Value is: ' + S, mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)
这是一个使用Format
函数的示例。展示了如何以格式显示浮点值General
以及如何使用格式显示具有 2 位小数的相同值Fixed
。有关格式的更多信息,请参阅该Format
函数的 Delphi 帮助:
var
S: string;
Value: Single;
begin
Value := 1.2345;
S := Format('Value is: %g', [Value]);
MsgBox(S, mbInformation, MB_OK);
S := Format('Value is: %.2f', [Value]);
MsgBox(S, mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)