ColdFusion 模数函数返回错误值
我最近编写了一些代码,用于安排消息发送到我们的 SMTP 服务器之一。该数据存储在 MySQL 数据库中。该算法根据每小时限制来安排消息。这是算法的一部分,它在一个循环内根据需要调度尽可能多的消息。
<cfif hourlyLimit lte 60>
<cfset minutesPerMail = 60 / hourlyLimit>
<cfset minutesAdd = int(minutesPerMail)>
<cfset secondsAdd = round((minutesPerMail % 1) * 60)>
<cfset queueDate = #DateAdd('n', minutesAdd, queueDate)#>
<cfset queueDate = #DateAdd('s', secondsAdd, queueDate)#>
出于调试目的,我将以下代码附加到这部分...
<cfdump var="#minutesPerMail#"> (Outputs 1.421857...)
<cfdump var="#secondsAdd#"> (Outputs 0)
我最初怀疑可能存在舍入错误,因此,我添加了以下代码...
<cfset modTest = minutesPerMail % 1>
<cfdump var="#modTest#"> (Outputs 0)
如您所见,通过使用 modTest 变量,模函数无法正常工作,因为正确的输出将是 .421857... 。
编辑/解决方案:在 ColdFusion 中执行模数操作会返回一个整数,而不是浮点数,因此会自动将元素舍入为零。
I have recently written some code which schedules messages to send to one of our SMTP servers. This data is stored in an MySQL database. The algorithm schedules messages based on an hourly limit. Here is a portion of the algorithm, which is within a loop to schedule as many messages as required.
<cfif hourlyLimit lte 60>
<cfset minutesPerMail = 60 / hourlyLimit>
<cfset minutesAdd = int(minutesPerMail)>
<cfset secondsAdd = round((minutesPerMail % 1) * 60)>
<cfset queueDate = #DateAdd('n', minutesAdd, queueDate)#>
<cfset queueDate = #DateAdd('s', secondsAdd, queueDate)#>
For debugging purposes, I appended the following code to this portion...
<cfdump var="#minutesPerMail#"> (Outputs 1.421857...)
<cfdump var="#secondsAdd#"> (Outputs 0)
I originally suspect there may be a rounding error, and as such, I then added the following code...
<cfset modTest = minutesPerMail % 1>
<cfdump var="#modTest#"> (Outputs 0)
As you can see, by the use of the modTest variable, the modulus function is not working appropriately, as the proper output would be .421857... .
EDIT/SOLUTION: Performing a modulus action within ColdFusion returns an integer, not a float, and thus will automatically round the element to zero.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能把它改成这样吗:
Can't you just change it to this:
为了进一步解释 MOD,它返回余数或 0。将其视为 TRUE 或 FALSE,这样如果它返回 TRUE,则它有余数,但如果它返回 FALSE,则它没有余数。希望这有助于澄清为什么它返回整数而不是浮点数。
To explain MOD slightly further, it returns either a remainder or 0. Think of it as TRUE or FALSE so that if it returns TRUE, it has a remainder, but if it returns FALSE, it does not have a remainder. Hope this helps to clarify why it returns an integer and not a float.