如何检查变量是否为数字?
我正在尝试这个:
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
但不幸的是,它没有按预期工作。
答案 0 :(得分:7)
set a to 5
if class of a is integer then
display dialog "Yes! It's a number!"
end if
答案 1 :(得分:4)
a的整数将失败
set a to "5"
即使变量是数字但是作为文本输入,这也可以。
set a to "5"
try
set a to a as number
display dialog "Yes! It's a number!"
end try
答案 2 :(得分:0)
这是我的解决方案:
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