Macos 检查变量是否为数字:Applescript
如何检查变量是否为数字 我正在尝试这个:Macos 检查变量是否为数字:Applescript,macos,variables,numbers,applescript,Macos,Variables,Numbers,Applescript,如何检查变量是否为数字 我正在尝试这个: 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 但不幸的是,它并没有像预期的那样工作。如果您使用 set a to 5 if class of a is integer then dis
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
但不幸的是,它并没有像预期的那样工作。如果您使用
set a to 5
if class of a is integer then
display dialog "Yes! It's a number!"
end if
set a to "5"
即使变量是一个数字,但输入为文本,这也会起作用
set a to "5"
try
set a to a as number
display dialog "Yes! It's a number!"
end try
这是我的解决方案:
on is_number(number_string)
try
set number_string to number_string as number
return true
on error
return false
end try
end is_number
顺便说一句,这并不检查整数,它允许用户输入一个“实数”,例如
将a设置为“5.5”
。(顺便说一句,这篇文章写得好像是对我答案的评论。也许它应该是。)OP没有要求整数。“如何检查变量是否为数字?”@jpwagner一个很可能的用例是,输入来自不同的脚本,其中所有输入都是非类型化的。和我一样,我也从bash脚本获取输入。