gpt4 book ai didi

excel - Excel INT 到 Delphi 的翻译

转载 作者:行者123 更新时间:2023-12-02 07:32:21 27 4
gpt4 key购买 nike

我正在尝试将 Excel 工作表转换为 Delphi,并偶然发现了 Excel INT。Excel 帮助说 INT 向下舍入(向负无穷大)。Excel 单元格是:

C13-(24*INT(C13/24))

其中 C13 初始化为 -465.9862462,计算结果为 14.01375378

我能想到的最好的 Delphi 是:

C13 := -465.9862462; 
RoundMode := GetRoundMode;// Save current mode
SetRoundMode(rmDown);
C13 := C13 - (24 * (Round(C13 / 24)));
SetRoundMode(RoundMode);// Restore mode

这会产生正确的答案,但是有更好的方法吗?

最佳答案

Delphi 也有一个 Int() 函数。甚至有记录:

http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.Int

这基本上相当于 Excel 函数。它也会向下舍入,但不会朝负无穷大方向舍入。它向下舍入到 0。因此,对于负数,您必须减去 1.0:

function ExcelInt(N: Extended): Extended;
begin
Result := System.Int(N);
if (Result <> N) and (N < 0.0) then
Result := Result - 1.0;
end;

注意:

人们可能倾向于使用System.Math.Floor,因为它已经向下舍入到负无穷大,但它返回一个Integer,因此对于较大的值,您可能会出现整数溢出。请注意,在 Excel 中,每个数字都是 Double,因此其 INT() 也返回 Double

关于excel - Excel INT 到 Delphi 的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48609404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com