0

Hello guys i please need your help. I have values, most of them are numbers but some of them are strings. How can i check if value is string or number?

I already tried this code but when it reach string value i get error " attempt to perform arithmetic on local 'numberValue' (a nil value)"

function Config:IsNumber(value)
    if value ~=nill or value ~=nil then                 
        local numberValue = tonumber(value)
        if numberValue/numberValue ==1 then
            return true
        else
            return false
        end
    end
end
end
end
4
  • 1
    there is a function in lua called type, you can use it like type(value) == "number" and this will only be true if value is a number. Commented Dec 21, 2021 at 20:59
  • Thats good to know, thanks however i found out that all my values are strings but some of theme are numbers what can be "tonumber" and some of them are just chars. Commented Dec 21, 2021 at 21:04
  • I found solution strmatch(val,"%d") , thanks for help anyway Commented Dec 21, 2021 at 21:09
  • 1
    if this solved your problem you should not be asking "how to check wether a value is a string" as you obviously wanted to know wether a string contains a digit. if that solved your problem. Commented Dec 22, 2021 at 7:46

1 Answer 1

4

First of all let's start with errors in your code.

  • You have 2 ends to many.
  • it is nil, not nill. I'm not sure where you're going with that check.

Other issues / things to improve:

numberValue / numberValue == 1 does not make any sense. A number divided by itself always results in 1. So checking that is pointless.

Also instead of

if something == 1 then
  return true
else
  return false
end

Can simply be replaced by return something == 1. There is no need for a conditional statement here.

To your question:

To check wether a value is a string use type(value) == "string".

To check wether a value can be converted to a number use tonumber(value). It will either return a number or nil.

So according to your error the conversion to a number failed.

If you don't know for sure that your value can be converted to a number, you should ensure that tonumber succeeded before you do any operations on its return value.

local numberValue = tonumber(value)
if not numberValue then
  print("converting value to a number failed")
  return
end
-- at this point you know numberValue is a number

So if you wanted to write a function that ensures a string represents a number you could do something like this:

function IsNumber(value)
  return tonumber(value) and true or false
end
Sign up to request clarification or add additional context in comments.

2 Comments

As i wrote before this solved my problem strmatch(val,"%d") ,I needed to check if value in string are digits.Those 2 nills checks are there because api sometimes return "nill" and sometimes "nil" and conditional statement is there because i want my method return true or false and not number.My problem is solved ty anyway.
you don't get my point. somenumber == 1 resolves to true or false. not a number. so you're basically using a conditional statement to convert true to true and false to false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.