浮点数 – 在Inno Setup Pascal Script中将Round / Truncate浮点
发布时间:2020-12-15 09:28:53 所属栏目:大数据 来源:网络整理
导读:这看起来不像Inno Setup问题,但实际上与其有用的Pascal脚本有关. 我编写了一个代码来进行浮点计算. Height,DivisionOfHeightWidth,Width: Integer;Height := 1080;Width := 1920;DivisionOfHeightWidth := Width / Height;Log('The Division Of Height and W
这看起来不像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 我希望这个编译器输出改为: The Division Of Height and Width: 1.77 我不能将Height和Width声明为Extended,Single或Double,因为它们在大多数情况下作为Integer返回,所以我需要将这两个Integers转换为两个Singles. 做完之后: 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.77那样四舍五入是不可能的,我怎么能像1.78一样围绕它呢? 提前致谢. 解决方法
如果舍入是可以接受的,一个简单的解决方案是使用
Format function:
var Height,Width: Integer; DivisionOfHeightWidthF: Single; begin ... DivisionOfHeightWidthF := Single(Width) / Height; Log(Format('The Division Of Height and Width: %.2f',[DivisionOfHeightWidthF])); end; 有关格式字符串的详细信息,请参阅 请注意,格式使用特定于区域设置的数字格式(特别是小数分隔符). 如果你真的需要截断,你需要自己实现它: 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使用特定于语言环境的小数分隔符,即并不总是.. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |