【问题标题】:Unexpected behaviour of int() [duplicate]int() 的意外行为 [重复]
【发布时间】:2024-08-09 21:05:02
【问题描述】:

我对 ColdFusion 10 (10,0,20,282462) 中 int()-function 的以下行为感到有些困惑:

<cfset dummy = 100 - (5859 / (6510 / 100)) />
<cfoutput>
    dummy = #dummy#<br><br> <!--- 10 --->
    int(10) = #int(10)#<br> <!--- 10 --->
    int(dummy) = #int(dummy)# <!--- 9 --->
</cfoutput>

谁能解释一下为什么 int(dummy) 返回 9 而不是 10?

【问题讨论】:

标签: coldfusion int rounding coldfusion-10


【解决方案1】:

int(dummy) 返回9 而不是10,因为它在其他语言中本质上是floor(),而您的答案可能会变成9,因为出于性能考虑,默认情况下它们被视为double

你听说过PrecisionEvaluate()https://cfdocs.org/precisionevaluate

dummy = PrecisionEvaluate(100 - (5859 / (6510 / 100)));
writeOutput(dummy);

您将按预期获得10

【讨论】: