确定字符串是否不为空/空白并且是数字而不是 0?

发布于 2024-12-09 13:50:30 字数 252 浏览 4 评论 0原文

我通常不在 ColdFusion 中工作,但工作中有一个 FTP 进程,我必须为其创建报​​告,目前唯一的选择是 ColdFusion 8 服务器。这个 FTP 源有一些问题(也是垃圾)。

因此,我进行查询,然后需要在输出期间转换一些字符串值以进行一些数学运算。在此之前:

我如何判断输出循环中的字段:不是空白或空,是可以转换为有效数字的字符串,并且不是0?

有没有一种简单的方法可以不用大量 if 语句来做到这一点?

谢谢!

I normally don't work in ColdFusion but there's a FTP process at work I have to create a report for with the only option right now being a ColdFusion 8 server. This FTP feed has a few issues (trash too).

So, I make the query and then I need to convert some of the string values during the output to do some math. Before that:

How do I tell if a field in the output loop: is not blank or null, is string that can be converted into a valid number, and is not 0?

Is there a simple way of doing this w/o a lot of if statements?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

檐上三寸雪 2024-12-16 13:50:30

那么您想确保变量是数字但不为零?

那么你想要这个:

<cfif IsNumeric(MyVar) AND MyVar NEQ 0 >

So you want to make sure that the variable is numeric but not zero?

Then you want this:

<cfif IsNumeric(MyVar) AND MyVar NEQ 0 >
云雾 2024-12-16 13:50:30

确定字符串是否不为空/空白并且是数字而不是 0?

这是我在本例中使用的代码。

<cfif isDefined(stringVar) and len((trim(stringVar))) and isNumeric(stringVar)>
    do stuff here
</cfif>

如果变量存在,isDefined 返回 true。如果您知道变量的范围,即例如在表单或 url 范围中,则可以使用 structkeyExists(form,"stringVar")。如果您知道变量的范围,我建议使用这种方法。

Len(trim(stringVar)) 是第二个检查。首先,它会修剪字符串中的任何前导或尾随空格 - 这确保不会传递任何空变量。然后,如果有东西,它将返回字符串的长度。如果其空 len 将返回 0。

如果变量是数字,则 isNumeric(stringVar) 返回 true,否则返回 false。

Determining if a string is not null/blank and is a number and not 0?

Here's the code I would use in this case.

<cfif isDefined(stringVar) and len((trim(stringVar))) and isNumeric(stringVar)>
    do stuff here
</cfif>

isDefined returns a true if the variable exists. If you know the scope of the variable, i.e., its in the form or url scope for instance, you can use structkeyExists(form,"stringVar"). I would recommend using this approach if you know the scope of the variable.

Len(trim(stringVar)) is the second check. First off it trims any leading or trailing empty spaces from the string - this makes sure that any empty variables are not passed along. Then if something is there it will return the length of the string. If its empty len will return a 0.

isNumeric(stringVar) returns a true if the variable is a number and false otherwise.

欢烬 2024-12-16 13:50:30
<cfif Len(field) and Val(field)>

Len() 将验证字段的长度(不是空白——CF 中没有 NULL),Val() 会自动将字符串中的第一个字符转换为数字——如果不能,则返回 0。

请注意下面彼得的评论;尽管这是最简单的答案,但 Val() 在下面的某些边缘条件下可能会失败,即。该字段是一个字符串,但以数字开头,错误地将其转换为数字,并评估为 TRUE。

<cfif Len(field) and Val(field)>

Len() will verify the field has length (not blank--there are no NULLs in CF) and Val() will automatically convert the first character in the string into into a number--or return 0 if it cannot.

Take note of Peter's comment below; although this is the least verbose answer, Val() may fail in certain edge conditions below, ie. The field is a string but starts with a number, incorrectly converting it to a number, and evaluating to TRUE.

墨小沫ゞ 2024-12-16 13:50:30
<cfif isNumeric(myfield) and myfield gt 0>
<cfif isNumeric(myfield) and myfield gt 0>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文