FLOAT data type

2023-12-25 08:49:42  Updated

The FLOAT data type is a subtype of the NUMBER data type. It can be specified with or without precision, and its definition is the same as that for NUMBER. This topic describes the syntax, parameters, and considerations of the FLOAT data type.

Syntax

FLOAT[(p)]

Parameters

Parameter Definition Value range Description
p Precision [1,126] The precision, which is the number of significant binary digits. To convert from binary precision to decimal precision, multiply the precision by 0.30103.

Note

  • Formula for conversion from binary precision to decimal precision: Decimal precision = int(binary precision × 0.30103).
  • Formula for conversion from decimal precision to binary precision: Binary precision = int(decimal precision × 3.32193).

Considerations

FLOAT is a subtype of the NUMBER data type and has a precision. A floating-point value takes 4 to 40 bytes. The precision can range from 1 to 126 binary digits. The number of decimal digits cannot be customized. FLOAT is an inexact numeric data type that has a variable length.

Examples

  • Use FLOAT to set the binary precision to 2, convert it to decimal precision: int(2 x 0.30103) = 0.6, and round down the result. The decimal precision of FLOAT(2) is 0.

    FLOAT(2)
    
  • Create a table named test and insert data into the table. The col1 column is of the NUMBER type and the col2 column is of the FLOAT type. NUMBER(5,2) means fixed-point numbers with decimal precision. The number of significant bits is 5 and the result is rounded to two places after the decimal point. FLOAT(5) means the binary precision of 5. You can convert it to decimal precision: int(5 x 0.30103) = 1.50515. The decimal precision is rounded down to 1. For example, the decimal number 123.45 is represented in decimal scientific notation as 1.2345 x 10^2^. If we round 1.2345 to one decimal place, we get 1.2. The final result displayed is 1.2 x 10^2^= 120. Execute the following statements:

    CREATE TABLE test (col1 NUMBER(5,2), col2 FLOAT(5));
    INSERT INTO test VALUES (1.23, 1.23);
    INSERT INTO test VALUES (7.89, 7.89);
    INSERT INTO test VALUES (12.79, 12.79);
    INSERT INTO test VALUES (123.45, 123.45);
    

    Execute the SELECT statement to view the test table.

    obclient> SELECT * FROM test;
    +--------+------+
    | COL1   | COL2 |
    +--------+------+
    |   1.23 |  1.2 |
    |   7.89 |  7.9 |
    |  12.79 |   13 |
    | 123.45 |  120 |
    +--------+------+
    4 rows in set
    

    Note

    To convert the ANSI FLOAT data, you can use the FLOAT data type used internally in OceanBase Database. However, we recommend that you use the BINARY_FLOAT and BINARY_DOUBLE floating-point data types.

Contact Us