Qore Programming Language Reference Manual  0.9.3.2
<int> Class Reference

Methods in this pseudo-class can be executed on integer values. More...

Inheritance diagram for Qore::<int>:

Public Member Methods

int abs ()
 Returns the absolute value of the number. More...
 
binary encodeLsb (int size=4)
 returns a binary object with the integer encoded in the given number of bytes in least significant byte order More...
 
binary encodeMsb (int size=4)
 returns a binary object with the integer encoded in the given number of bytes in most significant byte order More...
 
string format (string fmt)
 Returns a formatted string of the integer according to the format argument. More...
 
string format (int prec=2, string decimal_sep=".", string thousands_sep="")
 Returns a formatted string of the integer according to format arguments. More...
 
bool intp ()
 Returns True by default. More...
 
int sign ()
 Returns -1 if the number is negative, 0 if it is zero, or 1 if it is positive. More...
 
bool strp ()
 Returns True because integer values can be converted to strings. More...
 
string toBase (int base=10)
 Converts the integer to a different base (and returns it as a string). More...
 
string toUnicode ()
 Returns a single character string in UTF-8 encoding for the integer value treated as a unicode value. More...
 
int typeCode ()
 Returns Qore::NT_INT. More...
 
bool val ()
 Returns True if the int is non-zero, False if zero. More...
 
- Public Member Methods inherited from <value>
bool callp ()
 Returns False; this method is reimplemented in other types and will return True if the given expression is a callable value (ie closures or call references) More...
 
bool complexType ()
 returns True if the value has a complex type, False if not More...
 
bool empty ()
 Returns True; this method will be reimplemented in container types where it may return False. More...
 
string fullType ()
 returns the full type name which differs from the simple type name in case of complex types and objects More...
 
bool intp ()
 Returns False; this method is reimplemented in other types and will return True if the given expression can be converted to an integer. More...
 
AbstractIterator iterator ()
 Returns an iterator object for the value; the default iterator object returned is SingleValueIterator. More...
 
int lsize ()
 Returns 1; the return value of this method should give the list size of the value, which is normally 1 for non-lists (except for NOTHING where the size will be 0) and the number of the elements in the list for lists; this method will be reimplemented in other types where it may return other values. More...
 
int size ()
 Returns zero; this method will be reimplemented in container types where it may return a non-zero value. More...
 
bool sizep ()
 Returns True if the type can return a non-zero size (True for containers including binary objects and strings, False for everything else) More...
 
bool strp ()
 Returns False; this method is reimplemented in other types and will return True if the given expression can be converted to a string. More...
 
bool toBool ()
 Returns the boolean representation of the value; the default is False. More...
 
float toFloat ()
 Returns the floating-point representation of the value; the default is 0.0. More...
 
int toInt ()
 Returns the integer representation of the value; the default is 0. More...
 
number toNumber ()
 Returns the arbitrary-precision numeric representation of the value; the default is 0. More...
 
string toString ()
 Returns the string representation of the value; the default is an empty string. More...
 
string type ()
 Returns the string type for the value. More...
 
int typeCode ()
 Returns the type code for the value. More...
 
bool val ()
 Returns False; this method is reimplemented in other types and will return True if the given expression has a non-empty value. More...
 

Detailed Description

Methods in this pseudo-class can be executed on integer values.

Member Function Documentation

◆ abs()

int <int>::abs ( )

Returns the absolute value of the number.

Code Flags:
CONSTANT
Example:
i = i.abs();
Returns
the absolute value of the number
Note
equivalent to abs(int)
Since
Qore 0.8.8

◆ encodeLsb()

binary <int>::encodeLsb ( int  size = 4)

returns a binary object with the integer encoded in the given number of bytes in least significant byte order

Code Flags:
RET_VALUE_ONLY
Example:
b += i.encodeLsb(8);
Parameters
sizethe size of the encoded binary object; must be 1, 2, 4, or 8 or a SIZE-ERROR exception is raised
Returns
a binary object with the integer encoded in the given number of bytes in least significant byte order
Exceptions
SIZE-ERRORthe argument is not 1, 2, 4, or 8 or the argument cannot fit in the number of bytes requested
Since
Qore 0.8.8

◆ encodeMsb()

binary <int>::encodeMsb ( int  size = 4)

returns a binary object with the integer encoded in the given number of bytes in most significant byte order

Code Flags:
RET_VALUE_ONLY
Example:
b += i.encodeMsb(8);
Parameters
sizethe size of the encoded binary object; must be 1, 2, 4, or 8 or a SIZE-ERROR exception is raised
Returns
a binary object with the integer encoded in the given number of bytes in most significant byte order
Exceptions
SIZE-ERRORthe argument is not 1, 2, 4, or 8 or the argument cannot fit in the number of bytes requested
Since
Qore 0.8.8

◆ format() [1/2]

string <int>::format ( string  fmt)

Returns a formatted string of the integer according to the format argument.

Code Flags:
CONSTANT
Parameters
fmtthe format string has the following format:
<thousands_separator>[<decimal_separator>[<decimals>]]
where:
  • thousands_separator and decimal_separator are single characters defining the thousands and decimal separator characters respectively, and
  • decimals is one or more digits defining how may decimals should appear after the decimal point - if this is non-zero then the decimals will all be "0"; a negative number means to round that number of digits before the decimal place
Returns
a string of the integer according to a format string; if the format string does not follow the given format, then the number without formatting is returned
Example:
int i = -48392093894;
string nstr = i.format(".,3"); # returns "-48.392.093.894,000"
See also
Since
Qore 0.8.6

◆ format() [2/2]

string <int>::format ( int  prec = 2,
string  decimal_sep = ".",
string  thousands_sep = "" 
)

Returns a formatted string of the integer according to format arguments.

Code Flags:
RET_VALUE_ONLY
Parameters
precthe number of digits after the decimal point; a negative value means to round that number of digits before the decimal place, note that a positive value here will result in the given number of zeros appearing after the decimal place
decimal_sepa single character that defines the decimal separator; an empty string means no decimal separator
thousands_sepa single character that defines the thousands and further separators every three digits before the decimal point; an empty string means no thousands separator
Returns
a string representing the integer according to the format arguments as described above
Example:
int i = -48392093894;
string nstr = i.format(3, ",", "."); # returns "-48.392.093.894,000"
See also
Since
Qore 0.8.13

◆ intp()

bool <int>::intp ( )

Returns True by default.

Returns
True by default
Code Flags:
CONSTANT
Example:
if (n.intp())
printf("%y: can be converted to an integer: %d\n", n, int(n));

◆ sign()

int <int>::sign ( )

Returns -1 if the number is negative, 0 if it is zero, or 1 if it is positive.

Returns
-1 if the number is negative, 0 if it is zero, or 1 if it is positive
Code Flags:
CONSTANT
Example:
printf("sign: %d\n", i.sign());
Since
Qore 0.8.6

◆ strp()

bool <int>::strp ( )

Returns True because integer values can be converted to strings.

Returns
True because integer values can be converted to strings
Code Flags:
CONSTANT
Example:
if (n.strp())
printf("%y: can be converted to a string: '%s'\n", n, string(n));

◆ toBase()

string <int>::toBase ( int  base = 10)

Converts the integer to a different base (and returns it as a string).

Code Flags:
RET_VALUE_ONLY
Parameters
basethe base to convert the integer to; this value must be 2 - 36 inclusive or an INVALID-BASE exception will be thrown
Returns
the converted value as a string
Example:
27.toBase(2)); // returns "11011"
033.toBase(2)); // returns "11011"
0x1B.toBase(2)); // returns "11011"
(-27).toBase(5)); // returns "-102"
(-0x1B).toBase(5)); // returns "-102"
27.toBase(8)); // returns "33"
033.toBase(8)); // returns "33"
27.toBase()); // returns "27"
033.toBase()); // returns "27"
0x1B.toBase()); // returns "27"
(-27).toBase(13)); // returns "-21"
27.toBase(16)); // returns "1B"
0x1B.toBase(16)); // returns "1B"
27.toBase(27)); // returns "10"
27.toBase(28)); // returns "R"
(-27).toBase(36)); // returns "-R"
35.toBase(36)); // returns "Z"
Exceptions
INVALID-BASEthe base is invalid; must be 2 - 36 inclusive
See also
<number>::toBase(int base)
<float>::toBase(int base)
Since
Qore 0.8.13

◆ toUnicode()

string <int>::toUnicode ( )

Returns a single character string in UTF-8 encoding for the integer value treated as a unicode value.

Code Flags:
RET_VALUE_ONLY
Example:
string str = 0x00f8.toUnicode()
Returns
a single character string in UTF-8 encoding for the integer value treated as a unicode value
Note
only the least-significant 4 bytes of the integer are used
Since
Qore 0.8.8

◆ typeCode()

int <int>::typeCode ( )

Returns Qore::NT_INT.

Returns
Qore::NT_INT
Code Flags:
CONSTANT
Example:
switch (i.typeCode()) {
case NT_INT:
printf("%y: is a int\n", i);
break;
}

◆ val()

bool <int>::val ( )

Returns True if the int is non-zero, False if zero.

Returns
True if the int is non-zero, False if zero
Code Flags:
CONSTANT
Example:
bool b = i.val();
See also