Is there any livecode function or code that can detect if a variable is a whole number?
Code: Select all
Put a * b into c
If c is a whole number then
.......
Else
.......
End if
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Code: Select all
Put a * b into c
If c is a whole number then
.......
Else
.......
End if
Code: Select all
if c = trunc(c) then
answer "This is a whole number"
else
answer "This is not a whole number"
end if
Hi,Opaquer wrote: ↑Fri Sep 04, 2020 1:34 amThe trunc function is super handy in that it removes everything after a decimal point, so super useful if you only need to look at the number before the decimal point. Because of that, you can do something like this to see if the trunc of a number is equal to a number, which means you've got a whole number on your hands!
That should do the trick for you! Not sure if there's better ways to do it, but that should definitely be a great starting pointCode: Select all
if c = trunc(c) then answer "This is a whole number" else answer "This is not a whole number" end if
Code: Select all
Put 18.80 into c
if c = trunc(c) then
answer "This is a whole number"
else
answer "This is not a whole number"
end if
Implying that zeroes can be anywhere in the string, like "00011000". But it also categorically states that a decimal point, which is NOT a digit, is not allowed. I actually agree with this completely, that "1.0" is not an integer, though "1" certainly is. This is semantics, I suppose, but technically the dictionary is wrong.If the value contains digits (and an optional minus sign) and no other characters, it is an integer.
Agree it’s low priority but unless reported it will never get addressed... if no one has submitted a bug report I’ll do it at some point after work.
I think the programmatic equivalent of “whole number” is the unsigned Integer (i.e. a positive integer).
Sure, I assume you mean a function for tNumber like:There's an easy workaround
Code: Select all
if tNumber is an integer then return trunc(tNumber)
Actually you probably have a point there... as variable typing isn't a thing in livecode it won't really affect any arithmetical operation.
Code: Select all
if tNumber is an integer and "." is not in tNumber then
But then "003" would pass as an integer. By using "trunc", that oddity is removed, as well as ".".if tNumber is an integer and "." is not in tNumber then