Loading...
Searching...
No Matches
<number> Class Reference

Methods in this pseudo-class can be executed on arbitrary precision number values. More...

`#include <Pseudo_QC_Number.dox.h>`

Inheritance diagram for Qore::<number>:
[legend]

## Public Member Functions

number | abs ()
| Returns the absolute value of the number.
string | format (string fmt)
| Returns a formatted string of the number according to the format argument.
string | format (int prec=2, string decimal_sep=".", string thousands_sep="")
| Returns a formatted string of the number according to format arguments.
bool | infp ()
| Returns True if the number is infinity (+ or -)
bool | intp ()
| Returns True because number values can be converted to integers.
bool | nanp ()
| Returns True if the number is NaN (not a number)
int | prec ()
| Returns the precision of the current number.
int | sign ()
| Returns -1 if the number is negative, 0 if it is zero, or 1 if it is positive.
bool | strp ()
| Returns True because number values can be converted to strings.
string | toBase (int base=10)
| Converts the number to a different base (and returns it as a string).
string | toString (int fmt)
| Returns the string representation of the number according to the format argument.
int | typeCode ()
| Returns Qore::NT_NUMBER.
bool | val ()
| Returns True if the number is non-zero, False if zero.
Public Member Functions 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)
bool | complexType ()
| returns True if the value has a complex type, False if not
bool | empty ()
| Returns True; this method will be reimplemented in container types where it may return False.
string | fullType (*bool with_namespaces)
| returns the full type name which differs from the simple type name in case of complex types and objects
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.
AbstractIterator | iterator ()
| Returns an iterator object for the value; the default iterator object returned is SingleValueIterator.
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.
int | size ()
| Returns zero; this method will be reimplemented in container types where it may return a non-zero value.
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)
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.
bool | toBool ()
| Returns the boolean representation of the value; the default is False.
float | toFloat ()
| Returns the floating-point representation of the value; the default is 0.0.
int | toInt ()
| Returns the integer representation of the value; the default is 0.
number | toNumber ()
| Returns the arbitrary-precision numeric representation of the value; the default is 0.
string | toString ()
| Returns the string representation of the value; the default is an empty string.
string | type ()
| Returns the string type for the value.
int | typeCode ()
| Returns the type code for the value.
bool | val ()
| Returns False; this method is reimplemented in other types and will return True if the given expression has a non-empty value.

## Detailed Description

Methods in this pseudo-class can be executed on arbitrary precision number values.

## Member Function Documentation

## abs()

number <number>::abs | ( |  | ) |

Returns the absolute value of the number.

Code Flags:
  CONSTANT

Example:
  n = n.abs();

Returns
  the absolute value of the number

Note
  equivalent to abs(number)

Since
  Qore 0.8.8

## format() [1/2]

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

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

Code Flags:
  RET_VALUE_ONLY

Parameters

  prec | the number of digits after the decimal point; a negative value means to round that number of digits before the decimal place, whereas the special constant SNF_AllDigits means to include all possible digits after the decimal point
  decimal_sep | a single character that defines the decimal separator; an empty string means no decimal separator
  thousands_sep | a 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 number according to the format arguments as described above

Example:
  float f = -48392093894.2349;
  string fstr = f.format(3, ".", ","); # returns "-48.392.093.894,235"

See also
  - <float>::format(int, string, string)
  - <int>::format(int, string, string)
  - <number>::format(string)
  - <float>::format(string)
  - <int>::format(string)

Since
  Qore 0.8.13

## format() [2/2]

string <number>::format | ( | string | fmt | ) |

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

Code Flags:
  RET_VALUE_ONLY

Parameters

  fmt

  the 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; a negative number means to round that number of digits before the decimal place

Returns
  a string of a formatted number according to a format string; if the format string does not follow the given format, then the number without formatting is returned

Example:
  float f = -48392093894.2349;
  string fstr = f.format(".,3"); # returns "-48.392.093.894,235"

See also
  - <float>::format(string)
  - <int>::format(string)
  - <number>::format(int, string, string)
  - <float>::format(int, string, string)
  - <int>::format(int, string, string)

Since
  Qore 0.8.6

## infp()

bool <number>::infp | ( |  | ) |

Returns True if the number is infinity (+ or -)

Returns
  True if the number is infinity (+ or -)

Code Flags:
  CONSTANT

Example:
  if (n.infp())
  print("the operation resulted in infinity\n");

Since
  Qore 0.8.6

## intp()

bool <number>::intp | ( |  | ) |

Returns True because number values can be converted to integers.

Returns
  True because number values can be converted to integers

Code Flags:
  CONSTANT

Example:
  if (n.intp())
  printf("%y: can be converted to an integer: %d\n", n, int(n));

Since
  Qore 0.8.6

## nanp()

bool <number>::nanp | ( |  | ) |

Returns True if the number is NaN (not a number)

Returns
  True if the number is NaN (not a number)

Code Flags:
  CONSTANT

Example:
  if (n.nanp())
  print("the operation resulted in NaN\n");

Since
  Qore 0.8.6

## prec()

int <number>::prec | ( |  | ) |

Returns the precision of the current number.

Returns
  the precision of the current number

Code Flags:
  CONSTANT

Example:
  printf("precision: %d\n", n.prec());

Since
  Qore 0.8.6

## sign()

int <number>::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", n.sign());

Since
  Qore 0.8.6

## strp()

bool <number>::strp | ( |  | ) |

Returns True because number values can be converted to strings.

Returns
  True because number 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));

Since
  Qore 0.8.6

## toBase()

string <number>::toBase | ( | int | base = `10` | ) |

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

Code Flags:
  RET_VALUE_ONLY

Parameters

  base | the base to convert the number to; this value must be 2 - 36 inclusive or an `INVALID-BASE` exception will be thrown

Returns
  the converted value as a string

Example:
  (-276536798792387623n).toBase(29)); // returns "-MJ934KRG1O3R"
  276536798792387623n.toBase(2)); // returns "1111010110011101001100000011011011010110000000110000100111"
  (15.5n).toBase(20)); // returns "F.A"
  (-13.8n).toBase(20)); // returns "-D.G"
  (21.48n).toBase(15)); // returns "16.73"

Exceptions

  INVALID-BASE | the base is invalid; must be 2 - 36 inclusive

See also
  <int>::toBase(int base)
  <float>::toBase(int base)

Since
  Qore 0.8.13

## toString()

string <number>::toString | ( | int | fmt | ) |

Returns the string representation of the number according to the format argument.

Code Flags:
  CONSTANT

Parameters

  fmt | see Number Formatting Constants for possible values; note that this argument is interpreted as a bit field; if the format argument is not a valid format value (i.e. if none of the formatting bits are set in the argument) then NF_Default is assumed instead

Returns
  the string representation of the number according to the format argument

Since
  Qore 0.8.6

## typeCode()

int <number>::typeCode | ( |  | ) |

Returns Qore::NT_NUMBER.

Returns
  Qore::NT_NUMBER

Code Flags:
  CONSTANT

Example:
  switch (f.typeCode()) {
  case NT_NUMBER:
  printf("%y: is a number\n", f);
  break;
  }

Since
  Qore 0.8.6

## val()

bool <number>::val | ( |  | ) |

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

Returns
  True if the number is non-zero, False if zero

Code Flags:
  CONSTANT

Example:
  bool b = f.val();

See also
  - %perl-bool-eval
  - %strict-bool-eval

Since
  Qore 0.8.6