检查变量是否为数字:Applescript [英] Check if variable is number: Applescript

查看:29
本文介绍了检查变量是否为数字:Applescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查变量是否为数字?

How can I check if a variable is a number?

我正在尝试:

set a to 5
if a is a number
display dialog "Yes! It's a number!"
end if

我也试过这个代码:

set a to 5
if a is integer
display dialog "Yes! It's a number!"
end if

但不幸的是它没有按预期工作.

But unfortunately it doesn't work as expected.

推荐答案

class of a is integer 如果使用会失败

class of a is integer will fail if you use

set a to "5"

即使变量是数字但作为文本输入,这也会起作用.

This will work if even if the variable is a number but was entered as text.

set a to "5"
try
    set a to a as number
    display dialog "Yes! It's a number!"
end try

这篇关于检查变量是否为数字:Applescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆