在 Inno Setup Pascal 脚本中将浮点数舍入/截断为 N 位小数
Round / Truncate floating point numbers to N decimal places in Inno Setup Pascal Script
这看起来不像 Inno Setup 问题,但实际上与其有用的 Pascal 脚本有关。
我写了一个代码来进行浮点计算。
Height, DivisionOfHeightWidth, Width: Integer;
Height := 1080;
Width := 1920;
DivisionOfHeightWidth := Width / Height;
Log('The Division Of Height and Width: ' + IntToStr(DivisionOfHeightWidth));
编译器日志给出输出:
The Division Of Height and Width: 1
我希望这个编译器输出到 return 这个:
The Division Of Height and Width: 1.77
我无法将 Height
和 Width
声明为 Extended , Single
或 Double
,因为它们 returning 为 Integer
在大多数情况下,所以我需要将这两个整数转换为两个单数。
完成后:
Height, Width: Integer;
HeightF, WidthF, DivisionOfHeightWidthF: Single;
Height := 1080;
Width := 1920;
HeightF := Height;
WidthF := Width;
DivisionOfHeightWidthF := WidthF / HeightF;
Log('The Division Of Height and Width: ' + FloatToStr(DivisionOfHeightWidthF));
编译器日志现在给出输出:
The Division Of Height and Width: 1.777777791023
但是我怎样才能得到这个输出 1.77
? (四舍五入不是 1.78
)
我的意思是如何将 1.777777791023
舍入到小数点后两位,例如 1.77
?
如果无法像 1.77
那样四舍五入,我怎样才能像 1.78
那样四舍五入?
提前致谢。
要截断第 nth 十进制数字的值,只需将其乘以 10n,截断小数部分,然后除以 10 n 再
DivisionOfHeightWidthF := WidthF / HeightF;
DivisionOfHeightWidthF := Trunc(DivisionOfHeightWidthF*100.0)/100.0;
但是有一个问题是在截取步骤中,稍微低于整数值的值将被截取得更远,所以你需要在除以[=14=之前用round()
得到正确的整数]
DivisionOfHeightWidthF := round(int(DivisionOfHeightWidthF*100.0)))/100.0;
相关:How can I round an integer to the nearest 1000 in Pascal?
如果可以接受舍入,一个简单的解决方案是使用 Format
function:
var
Height, Width: Integer;
DivisionOfHeightWidthF: Single;
begin
...
DivisionOfHeightWidthF := Single(Width) / Height;
Log(Format('The Division Of Height and Width: %.2f', [DivisionOfHeightWidthF]));
end;
有关格式字符串的详细信息,请参阅 Format
function 的 Delphi 文档。
请注意,Format
使用特定于语言环境的数字格式(特别是小数点分隔符)。
如果你真的需要截断,你需要像这样自己实现:
var
Height, Width: Integer;
DivisionOfHeightWidthF: Single;
S: string;
P: Integer;
begin
...
DivisionOfHeightWidthF := Single(Width) / Height;
S := FloatToStr(DivisionOfHeightWidthF);
P := Pos('.', S);
if P < Length(S) - 2 then
begin
SetLength(S, P + 2);
end;
Log(S);
end;
以上仅在 Unicode Inno Setup 中有效,因为在 Ansi 版本中 FloatToStr
使用区域设置特定的小数点分隔符,即不总是 .
。在目前的Inno Setup 6中,Unicode版本是唯一的版本。
这看起来不像 Inno Setup 问题,但实际上与其有用的 Pascal 脚本有关。
我写了一个代码来进行浮点计算。
Height, DivisionOfHeightWidth, Width: Integer;
Height := 1080;
Width := 1920;
DivisionOfHeightWidth := Width / Height;
Log('The Division Of Height and Width: ' + IntToStr(DivisionOfHeightWidth));
编译器日志给出输出:
The Division Of Height and Width: 1
我希望这个编译器输出到 return 这个:
The Division Of Height and Width: 1.77
我无法将 Height
和 Width
声明为 Extended , Single
或 Double
,因为它们 returning 为 Integer
在大多数情况下,所以我需要将这两个整数转换为两个单数。
完成后:
Height, Width: Integer;
HeightF, WidthF, DivisionOfHeightWidthF: Single;
Height := 1080;
Width := 1920;
HeightF := Height;
WidthF := Width;
DivisionOfHeightWidthF := WidthF / HeightF;
Log('The Division Of Height and Width: ' + FloatToStr(DivisionOfHeightWidthF));
编译器日志现在给出输出:
The Division Of Height and Width: 1.777777791023
但是我怎样才能得到这个输出 1.77
? (四舍五入不是 1.78
)
我的意思是如何将 1.777777791023
舍入到小数点后两位,例如 1.77
?
如果无法像 1.77
那样四舍五入,我怎样才能像 1.78
那样四舍五入?
提前致谢。
要截断第 nth 十进制数字的值,只需将其乘以 10n,截断小数部分,然后除以 10 n 再
DivisionOfHeightWidthF := WidthF / HeightF;
DivisionOfHeightWidthF := Trunc(DivisionOfHeightWidthF*100.0)/100.0;
但是有一个问题是在截取步骤中,稍微低于整数值的值将被截取得更远,所以你需要在除以[=14=之前用round()
得到正确的整数]
DivisionOfHeightWidthF := round(int(DivisionOfHeightWidthF*100.0)))/100.0;
相关:How can I round an integer to the nearest 1000 in Pascal?
如果可以接受舍入,一个简单的解决方案是使用 Format
function:
var
Height, Width: Integer;
DivisionOfHeightWidthF: Single;
begin
...
DivisionOfHeightWidthF := Single(Width) / Height;
Log(Format('The Division Of Height and Width: %.2f', [DivisionOfHeightWidthF]));
end;
有关格式字符串的详细信息,请参阅 Format
function 的 Delphi 文档。
请注意,Format
使用特定于语言环境的数字格式(特别是小数点分隔符)。
如果你真的需要截断,你需要像这样自己实现:
var
Height, Width: Integer;
DivisionOfHeightWidthF: Single;
S: string;
P: Integer;
begin
...
DivisionOfHeightWidthF := Single(Width) / Height;
S := FloatToStr(DivisionOfHeightWidthF);
P := Pos('.', S);
if P < Length(S) - 2 then
begin
SetLength(S, P + 2);
end;
Log(S);
end;
以上仅在 Unicode Inno Setup 中有效,因为在 Ansi 版本中 FloatToStr
使用区域设置特定的小数点分隔符,即不总是 .
。在目前的Inno Setup 6中,Unicode版本是唯一的版本。