# Floating-point cheat sheet for SQL

## Floating-Point Types

The SQL standard defines three binary floating-point types:

`REAL`

has implementation-dependent precision (usually maps to a hardware-supported type like IEEE 754 single or double precision)`DOUBLE PRECISION`

has implementation-dependent precision which is greater than`REAL`

(usually maps to IEEE 754 double precision)`FLOAT(N)`

has at least`N`

binary digits of precision, with an implementation-dependent maximum for`N`

The exponent range for all three types is implementation-dependent as well.

## Decimal Types

The standard defines two fixed-point decimal types:

`NUMERIC(M,N)`

has exactly`M`

total digits,`N`

of them after the decimal point`DECIMAL(M,N)`

is the same as`NUMERIC(M,N)`

, except that it is allowed to have more than`M`

total digits

The maximum values of `M`

and `M`

are implementation-dependent. Vendors often implement the two types identically.

## How to Round

The SQL standard defines no explicit rounding, but most vendors provide a `ROUND()`

or `TRUNC()`

function.

However, it usually makes little sense to round within the database, since its job is *storing* data, while rounding is an aspect of *displaying* data, and should therefore be done by the code in the presentation layer.

## Resources

- Official ISO SQL 2008 standard (non-free)
- SQL 92 draft (free)
- MySQL numeric types
- PostgreSQL Numeric Types
- MS SQL Server data types

© Published at floating-point-gui.de under the Creative Commons Attribution License (BY)