The Number
class handles limited-precision signed base 10
numbers. A Number
guarantees 38
decimal digits of precision. All positive numbers in the range displayed here can be represented to a full 38
-digit precision:
10^-130
and
9.99999999999999999999999999999999999999*10^125
The range of representable negative numbers is symmetrical.
The number zero can be represented exactly. Also, Oracle numbers have representations for positive and negative infinity. These are generally used to indicate overflow.
The internal storage type is opaque and private. Scale is not preserved when Number
instances are created.
Number
does not support the concept of NaN and is not IEEE-754-85 compliant. Number
does support +Infinity and -Infinity.
Objects from the Number
class can be used as standalone class objects in client side numeric computations. They can also be used to fetch from and set to the database.
Example 13-10 How to Retrieve and Use a Number Object
This example demonstrates a Number
column value being retrieved from the database, a bind using a Number
object, and a comparison using a standalone Number
object.
/* Create a connection */ Environment *env = Environment::createEnvironment(Environment::DEFAULT); Connection *conn = Connection(user, passwd, db); /* Create a statement and associate a select clause with it */ string sqlStmt = "SELECT department_id FROM DEPARTMENTS"; Statement *stmt = conn->createStatement(sqlStmt); /* Run the statement to get a result set */ ResultSet *rset = stmt->executeQuery(); while(rset->next()) { Number deptId = rset->getNumber(1); /* Display the department id with the format string 9,999 */ cout << "Department Id" << deptId.toText(env, "9,999"); /* Use the number obtained as a bind value in the following query */ stmt->setSQL("SELECT * FROM EMPLOYEES WHERE department_id = :x"); stmt->setNumber(1, deptId); ResultSet *rset2 = stmt->executeQuery(); . . } /* Using a Number object as a standalone and the operations on them */ /* Create a number to a double value */ double value = 2345.123; Number nu1 (value); /* Some common Number methods */ Number abs = nu1.abs(); /* absolute value */ Number sqrt = nu1.squareroot(); /* square root */ Environment *env = Environment::createEnvironment(); //create a null year-month interval IntervalYM ym if(ym.isNull()) cout << "\n ym is null"; //assign a non null value to ym IntervalYM anotherYM(env, "10-30"); ym = anotherYM; //now all operations are valid on ym int yr = ym.getYear();
Table 13-30 Summary of Number Methods
Method | Summary |
---|---|
|
|
Returns the absolute value of the number. |
|
Returns the arcCosine of the number. |
|
Returns the arcSine of the number. |
|
Returns the arcTangent of the number. |
|
Returns the arcTangent2 of the input number |
|
Returns the smallest integral value not less than the value of the number. |
|
Returns the cosine of the number. |
|
Returns the natural exponent of the number. |
|
Returns the largest integral value not greater than the value of the number. |
|
Returns a Number derived from a |
|
Returns a Number from a given number string, format string and NLS parameters specified. |
|
Returns the hyperbolic cosine of the number. |
|
Returns the hyperbolic sine of the number. |
|
Returns the hyperbolic tangent of the number. |
|
Returns the number raised to the integer value specified. |
|
Checks if |
|
Returns the natural logarithm of the number. |
|
Returns the logarithm of the number to the base value specified. |
|
Increments the |
|
Decrements the n |
|
Returns the product of two |
|
Returns the quotient of two |
|
Returns the modulo of two |
|
Returns the sum of two |
|
Returns the negated value of |
|
Returns the difference between two |
|
Checks if a number is less than an other number. |
|
Checks if a number is less than or equal to an other number. |
|
Checks if a number is greater than an other number. |
|
Checks if a number is greater than or equal to an other number. |
|
Assigns one number to another. |
|
Checks if two numbers are equal. |
|
Checks if two numbers are not equal. |
|
Multiplication assignment. |
|
Division assignment. |
|
Modulo assignment. |
|
Addition assignment. |
|
Subtraction assignment. |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Sets |
|
Returns a |
|
Returns the sign of the value of the passed value: -1 for the passed value < 0, 0 for the passed value == 0, and 1 for the passed value > 0. |
|
Returns sine of the number. |
|
Returns the square root of the number. |
|
Returns tangent of the number. |
|
Returns a |
|
Returns the number as a string formatted based on the format and NLS parameters. |
|
Returns a |
Number
class constructor.
Parameter | Description |
---|---|
srcNum |
The source Number copied into the new Number object. |
val |
The value assigned to the Number object. |
This method returns the arctangent of the Number
object with the parameter specified. It returns atan2 (val, x)
where val
is the parameter specified and x
is the current number object.
const Number arcTan2( const Number &val) const;
Parameter | Description |
---|---|
val |
Number parameter val to the arcTangent function atan2(val,x) . |
This method returns the smallest integer that is greater than or equal to the Number
object.
const Number ceil() const;
This method returns the largest integer that is less than or equal to the Number
object.
const Number floor() const;
This method returns a Number
object represented by the byte string specified.
void fromBytes( const Bytes &str);
Parameter | Description |
---|---|
str |
A byte string. |
Sets Number
object to value represented by a string
or UString
.
The value is interpreted based on the fmt
and nlsParam
parameters. In cases where nlsParam
is not passed, the Globalization Support settings of the envp
parameter are used.
Syntax | Description |
---|---|
void fromText( const Environment *envp, const string &number, const string &fmt, const string &nlsParam = ""); |
Sets Number object to value represented by a string . |
void fromText( const Environment *envp, const UString &number, const UString &fmt, const UString &nlsParam); |
Sets Number object to value represented by a UString . |
Parameter | Description |
---|---|
envp |
The OCCI environment. |
number |
The number string to be converted to a Number object. |
fmt |
The format string. |
nlsParam |
The NLS parameters string. If nlsParam is specified, this determines the NLS parameters to be used for the conversion. If nlsParam is not specified, the NLS parameters are picked up from envp . |
This method returns a Number
whose value is the number object raised to the power of the value specified.
const Number intPower( int val) const;
Parameter | Description |
---|---|
val |
Power to which the number is raised. |
This method tests whether the Number
object is NULL
. If the Number
object is NULL
, then TRUE
is returned; otherwise, FALSE
is returned.
bool isNull() const;
This method returns the logarithm of the Number
object with the base provided by the parameter specified.
const Number log( const Number &val) const;
Parameter | Description |
---|---|
val |
The base to be used in the logarithm calculation. |
Unary operator++()
. This is a postfix operator.
Syntax | Description |
---|---|
Number& operator++(); |
This method returns the Number object incremented by 1 . |
const Number operator++( int incr); |
This method returns the Number object incremented by the integer specified. |
--
()Unary operator--()
. This is a prefix operator.
Syntax | Description |
---|---|
Number& operator--(); |
This method returns the Number object decremented by 1 . |
const Number operator--( int decr); |
This method returns the Number object decremented by the integer specified. |
Parameter | Description |
---|---|
decr |
The number by which the Number object is decremented. |
This method returns the product of the parameters specified.
Number operator*( const Number &first,
const Number &second);
Parameter | Description |
---|---|
first |
First multiplicand. |
second |
Second multiplicand. |
This method returns the quotient of the parameters specified.
Number operator/( const Number ÷nd, const Number &divisor);
Parameter | Description |
---|---|
dividend |
The number to be divided. |
divisor |
The number by which to divide. |
This method returns the remainder of the division of the parameters specified.
Number operator%( const Number ÷nd, const Number ÷r);
Parameter | Description |
---|---|
dividend |
The number to be divided. |
divizor |
The number by which to divide. |
This method returns the sum of the parameters specified.
Number operator+( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be added. |
second |
Second number to be added. |
Unary operator-()
. This method returns the negated value of the Number
object.
const Number operator-();
This method returns the difference between the parameters specified.
Number operator-( const Number &subtrahend, const Number &subtractor);
Parameter | Description |
---|---|
subtrahend |
The number to be reduced. |
subtractor |
The number to be subtracted. |
This method checks whether the first parameter specified is less than the second parameter specified. If the first parameter is less than the second parameter, then TRUE
is returned; otherwise, FALSE
is returned. If either parameter equals infinity, then FALSE
is returned.
bool operator<( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method checks whether the first parameter specified is less than or equal to the second parameter specified. If the first parameter is less than or equal to the second parameter, then TRUE
is returned; otherwise, FALSE
is returned. If either parameter
equals infinity, then FALSE
is returned.
bool operator<=( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method checks whether the first parameter specified is greater than the second parameter specified. If the first parameter is greater than the second parameter, then TRUE
is returned; otherwise, FALSE
is returned. If either parameter equals infinity, then FALSE
is returned.
bool operator>( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method checks whether the first parameter specified is greater than or equal to the second parameter specified. If the first parameter is greater than or equal to the second parameter, then TRUE
is returned; otherwise, FALSE
is returned. If either parameter equals infinity, then FALSE
is returned.
bool operator>=( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method checks whether the parameters specified are equal. If the parameters are equal, then TRUE
is returned; otherwise, FALSE
is returned. If either parameter equals +infinity or -infinity, then FALSE
is returned.
bool operator==( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method checks whether the first parameter specified equals the second parameter specified. If the parameters are not equal, TRUE
is returned; otherwise, FALSE
is returned.
bool operator!=( const Number &first, const Number &second);
Parameter | Description |
---|---|
first |
First number to be compared. |
second |
Second number to be compared. |
This method assigns the value of the parameter specified to the Number
object.
Number& operator=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
This method multiplies the Number
object by the parameter specified, and assigns the product to the Number
object.
Number& operator*=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
This method divides the Number
object by the parameter specified, and assigns the quotient to the Number
object.
Number& operator/=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
This method divides the Number
object by the parameter specified, and assigns the remainder to the Number
object.
Number& operator%=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
This method adds the Number object and the parameter specified, and assigns the sum to the Number
object.
Number& operator+=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
-=
()This method subtracts the parameter specified from the Number
object, and assigns the difference to the Number
object.
Number& operator-=( const Number &num);
Parameter | Description |
---|---|
num |
A parameter of type Number . |
This method returns the value of the Number object converted to a native char
.
operator char() const;
This method returns the value of the Number object converted to a native signed char
.
operator signed char() const;
This method returns the value of the Number
object converted to a native double
.
operator double() const;
This method returns the value of the Number
object converted to a native float
.
operator float() const;
This method returns the value of the Number
object converted to a native int
.
operator int() const;
This method returns the value of the Number
object converted to a native long
.
operator long() const;
This method returns the value of the Number
object converted to a native long double
.
operator long double() const;
This method returns the value of the Number
object converted to a native short
integer.
operator short() const;
This method returns the value of the Number
object converted to a native unsigned char
.
operator unsigned char() const;
This method returns the value of the Number
object converted to a native unsigned int
.
operator unsigned int() const;
This method returns the value of the Number
object converted to a native unsigned long
.
operator unsigned long() const;
This method returns the value of the Number
object converted to a native unsigned short
integer.
operator unsigned short() const;
This method returns the value of the Number
object raised to the power of the value provided by the parameter specified.
const Number power( const Number &val) const;
Parameter | Description |
---|---|
val |
The power to which the number has to be raised. |
This method returns the value of the Number
object rounded to the digits of precision provided by the parameter specified.
const Number prec( int digits) const;
Parameter | Description |
---|---|
digits |
The number of digits of precision. |
This method returns the value of the Number
object rounded to the decimal place provided by the parameter specified.
const Number round( int decPlace) const;
Parameter | Description |
---|---|
decPlace |
The number of digits to the right of the decimal point. |
This method returns the Number
object multiplied by 10 to the power provided by the parameter specified.
const Number shift( int val) const;
Parameter | Description |
---|---|
val |
An integer value. |
This method returns the sign of the value of the Number
object. If the Number
object is negative, then create a Date
object using integer parameters is returned. If the Number
object equals 0
, then create a Date
object using integer parameters is returned. If the Number
object is positive, then 1
is returned.
const int sign() const;
This method returns the square root of the Number
object.
const Number squareroot() const;
This method converts the Number
object into a Bytes
object. The bytes representation is assumed to be in length excluded format, that is, the Byte.length()
method gives the length of valid bytes and the 0
th byte is the exponent byte.
Bytes toBytes() const;
Convert the Number
object to a formatted string
or UString
based on the parameters specified.
Syntax | Description |
---|---|
string toText( const Environment *envp, const string &fmt, const string &nlsParam = "") const; |
Convert the Number object to a formatted string based on the parameters specified. |
UString toText( const Environment *envp, const UString &fmt, const UString &nlsParam) const; |
Convert the Number object to a UString based on the parameters specified. |
Parameter | Description |
---|---|
envp |
The OCCI environment. |
fmt |
The format string. |
nlsParam |
The NLS parameters string. If nlsParam is specified, this determines the NLS parameters to be used for the conversion. If nlsParam is not specified, the NLS parameters are picked up from envp . |