1. Fernando Leal
  2. PowerBuilder
  3. Tuesday, 28 April 2020 20:42 PM UTC

How can I check if a number is integer?
I have a string of characters XXXXXX and I get its length and at the same time I need to convert it and I want to check if it is an integer.

Example:
String ls_cadena = AAAAAA
Integer li_quantidad
li_quantity = len (ls_string)
MOD (li_quantity, 78) = X, how do I validate that the result of this is an integer?

 

Como yo puedo comprobar si un número es entero?
Tengo una cadena de caracteres XXXXXX y otengo su largo y a la vez necesito devidirlo y yo quiero comprobar si es un entero.

Ejemplo:
String ls_cadena = AAAAAA
Integer li_cantidad
li_cantidad = len(ls_cadena)
MOD(li_cantidad, 78) = X, cómo valido que el resultado de esto es un entero?

 

 

thanks and regards, 

 

John Fauss Accepted Answer Pending Moderation
  1. Wednesday, 29 April 2020 01:16 AM UTC
  2. PowerBuilder
  3. # 1

HI, Fernando -

When you say "integer", are you referring to a 16-bit value or something potentially larger?

You can use the Long(string) and LongLong(string) functions as an alternative to the Integer(string) function.

If you want to be a little more explicit, you could use the Match function:

String ls_value

ls_value = sle_1.Text

If Match(ls_value,'^\-[0-9]+') or Match(ls_value,'^\+[0-9]+') or Match(ls_value,'^[0-9]+') then
   MessageBox('Good Test','This is an integer value')
else
   MessageBox('Bad Test','This is NOT an integer value')
End if

The first Match function in the If-statement matches a whole number preceded by a minus sign. The second when preceded with a plus sign. The third when there is no plus or minus sign.

Comment
  1. Fernando Leal
  2. Wednesday, 29 April 2020 02:44 AM UTC
Hi John, very good explanation.

My intention is determined when the number is integer or decimal.

For example

Integer li_count



li_count =Mod(12,2) -》the result is integer or decimal, how can I check this?

Thank and regards.
  1. Helpful
  1. John Fauss
  2. Wednesday, 29 April 2020 04:37 AM UTC
Let me see if I understand: You are wanting to see if the result of a Modulo operation (the Mod function) is a whole number...that it has no decimal places. If both operands are whole numbers (Int, UInt, Long, ULong, LongLong) then the modulo (which is the remainder from the division of the two operands) should always be a whole number. That's why I'm a little confused by your question.



However, if non-integer-type datatypes are used in the Mod function, the results will likely not be represented by a whole number.



In that case, depending on the datatypes in question, I might cast/convert both Mod function operands to decimals, perform the Mod, then perform a Mod(x,1.0) [where "x" is the result of the original primary Mod function] and see if that result = 0.0 (decimal). For example:



If Mod(Mod(Dec(var1),Dec(var2)),1.0) = 0.0 Then MessageBox("Congratulations!","It's a whole number.")



HTH
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Tuesday, 28 April 2020 20:51 PM UTC
  2. PowerBuilder
  3. # 2

There is the function IsNumber but that returns true on decimals as well. The Integer function returns the number or zero if the string is not an integer number.

Comment
  1. Roland Smith
  2. Wednesday, 29 April 2020 00:33 AM UTC
Convert the string to integer and then back to string. Compare the two strings. If the two strings are the same, the value is an integer.
  1. Helpful
  1. Fernando Leal
  2. Wednesday, 29 April 2020 02:45 AM UTC
Hi Roland, My intention is determined when the number is integer or decimal.

For example

Integer li_count



li_count =Mod(12,2) -》the result is integer or decimal, how can I check this?

Thank and regards.
  1. Helpful
  1. Roland Smith
  2. Wednesday, 29 April 2020 03:07 AM UTC
When you assign a decimal to an integer, it is truncated to the whole number. Assigning 12.345 to an integer gives 12.



ls_string1 = "12.345"

ls_string2 = String(Integer(ls_string1))

if ls_string1 = ls_string2 then

// integer

else

// decimal or non-number

end if
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.