Download Oracle Database PL/SQL User's Guide and Reference

Transcript
Character Sets and Lexical Units
Numeric Literals
Two kinds of numeric literals can be used in arithmetic expressions: integers and reals.
An integer literal is an optionally signed whole number without a decimal point. Some
examples follow:
030
6
-14
0
+32767
A real literal is an optionally signed whole or fractional number with a decimal point.
Several examples follow:
6.6667
0.0
-12.0
3.14159
+8300.00
.5
25.
PL/SQL considers numbers such as 12.0 and 25. to be reals even though they have
integral values.
A numeric literal value that is composed only of digits and falls in the range
-2147483648 to 2147483647 has a PLS_INTEGER datatype; otherwise this literal has the
NUMBER datatype. You can add the f of d suffix to a literal value that is composed only
of digits to specify the BINARY_FLOAT or BINARY_TABLE respectively. For the
properties of the datatypes, see "PL/SQL Number Types" on page 3-2.
Numeric literals cannot contain dollar signs or commas, but can be written using
scientific notation. Simply suffix the number with an E (or e) followed by an
optionally signed integer. A few examples follow:
2E5
1.0E-7
3.14159e0
-1E38
-9.5e-3
E stands for times ten to the power of. As the next example shows, the number after E
is the power of ten by which the number before E is multiplied (the double asterisk
(**) is the exponentiation operator):
5E3 = 5 * 10**3 = 5 * 1000 = 5000
The number after E also corresponds to the number of places the decimal point shifts.
In the last example, the implicit decimal point shifted three places to the right. In this
example, it shifts three places to the left:
5E-3 = 5 * 10**-3 = 5 * 0.001 = 0.005
The absolute value of a NUMBER literal can be in the range 1.0E-130 up to (but not
including) 1.0E126. The literal can also be 0. See Example 2–1. For information on
results outside the valid range, see "NUMBER Datatype" on page 3-3.
Example 2–1 NUMBER Literals
DECLARE
n NUMBER; -- declare n of NUMBER datatype
BEGIN
n := -9.999999E-130; -- valid
n := 9.999E125; -- valid
-- n := 10.0E125; -- invalid, "numeric overflow or underflow"
END;
/
Real literals can also use the trailing letters f and d to specify the types
BINARY_FLOAT and BINARY_DOUBLE, as shown in Example 2–2.
Example 2–2 Using BINARY_FLOAT and BINARY_DOUBLE
DECLARE
x BINARY_FLOAT := sqrt(2.0f); -- single-precision floating-point number
y BINARY_DOUBLE := sqrt(2.0d); -- double-precision floating-point number
BEGIN
Fundamentals of the PL/SQL Language 2-5