Round / Truncate floating point numbers to N decimal places in Inno Setup Pascal Script
If rounding is acceptable, an easy solution is using the Format
function:
var
Height, Width: Integer;
DivisionOfHeightWidthF: Single;
begin
...
DivisionOfHeightWidthF := Single(Width) / Height;
Log(Format('The Division Of Height and Width: %.2f', [DivisionOfHeightWidthF]));
end;
For details on the format string, see Delphi documentation for the Format
function.
Note that the Format
uses a locale-specific number format (decimal separator particularly).
If you really need truncating, you need to implement it yourself like:
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;
The above works in Unicode Inno Setup only as in Ansi version the FloatToStr
uses locale-specific decimal separator, i.e. not always the .
. In current Inno Setup 6, the Unicode version is the only version.