round(v) then msgBox("<>") end -6ren">
gpt4 book ai didi

excel - 如何确定从字符串创建的变体是否是整数?

转载 作者:行者123 更新时间:2023-12-02 04:00:41 24 4
gpt4 key购买 nike

我正在确定从字符串创建的变体是否是整数。

这是一个测试脚本:

dim v as variant
v = "42"
if v <> round(v) then
msgBox("<>")
end if

消息框弹出,可能是因为变体是从字符串创建的,尽管我预计 v = round(v)。

最佳答案

你应该这样写:

if cDbl(v) <> round(cDbl(v)) Then

其中cDbl是将任何数据转换为 double 型数字的函数。在调用 cDbl 函数之前,您可能必须使用 isNumeric() 函数处理 v 无法转换为数字的情况。您甚至可以使用 cInt 函数进行比较:

if isnumeric(v) then
if cDbl(v) - cInt(v) <> 0 Then
....
endif
else
debug.print "data cannot be converted to a number"
endif

关于excel - 如何确定从字符串创建的变体是否是整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1795423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com