FLOAT (TSQL Datatype)
Is a 16-bytes double-precision floating-point number (base 2).
Syntax
FLOAT
Range
+-2.2250738585072014e–308 to +-1.7976931348623158e308
Remarks
This type can store numbers with 15 decimal significant digits.
As it is base 2, non-integer decimal numbers are not stored exactly. So, don’t use
float
to store currency and monetary values. Usenumeric
instead.
When you want to convert float
to varchar
, you can use FORMAT
or STR
to have more control on the formatting.
If result of an operation is NaN or Infinite, an error is raised.
Literal Float
Any literal number containing an exponent part e
is parsed as a float
type.
With RSQL, the suffix f
forces any literal number to be parsed as float
.
1234.56e0
20e-4
-678.125e0
123f -- RSQL only. Parsed as float literal.
123.50f -- RSQL only. Parsed as float literal.
Examples
DECLARE @a FLOAT = 123e0;
PRINT @a + 45.6e-1;
PRINT 1e200 * 1e200; -- result is Infinite, an error is raised
The result is:
127.56
<Arithmetic error. FLOAT is Infinite.>
Examples (accuracy)
PRINT 123e0 + 45.6e-1; -- 'float' literals
PRINT 123456789012345e0; -- 'float' literal
PRINT 1.7e0; -- 'float' literal
PRINT 123.3e0 / 10000e0 * 10000e0; -- calculation with 'float'
PRINT 123.3 / 10000 * 10000 ; -- calculation with 'numeric'
The result is:
127.56
1.23456789012345e+14
1.7 <--- in fact, this 'float' is stored internally as 1.6999999999999999
123.29999999999998 <--- result with 'float' is unaccurate
123.300000000000 <--- result with 'numeric' is accurate