Convert to float/double
From stringβ
Since version 4.0, the result of overflow is no longer NULL, but +/-Infinity.
Strict modeβ
If the source type is nullable, returns nullable type;
If the source type is non-nullable, returns non-nullable type;
BNF definitionβ
<float> ::= <whitespace>* <value> <whitespace>*
<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
<value> ::= <decimal> | <infinity> | <nan>
<decimal> ::= <sign>? <significand> <exponent>?
<infinity> ::= <sign>? <inf_literal>
<nan> ::= <sign>? <nan_literal>
<sign> ::= "+" | "-"
<significand> ::= <digits> | <digits> "." <digits> | <digits> "." | "." <digits>
<digits> ::= <digit>+
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<exponent> ::= <e_marker> <sign>? <digits>
<e_marker> ::= "e" | "E"
<inf_literal> ::= <"INF" case-insensitive> | <"INFINITY" case-insensitive>
<nan_literal> ::= <"NAN" case-insensitive>
Rule descriptionβ
-
Only supports decimal format numbers;
-
Supports scientific notation;
-
Numbers can be prefixed with positive or negative sign characters;
-
Strings allow arbitrary prefix and suffix whitespace characters, including: " ", "\t", "\n", "\r", "\f", "\v";
-
Supports Infinity and NaN;
-
Return error for other formats;
-
Overflow converts to +|-Infinity.
Examplesβ
String | float/double | Comment |
---|---|---|
"123.456" | 123.456 | |
"123456." | 123456 | |
"123456" | 123456 | |
".123456" | 0.123456 | |
" \t\r\n\f\v123.456 \t\r\n\f\v" | 123.456 | With prefix and suffix whitespace |
" \t\r\n\f\v+123.456 \t\r\n\f\v" | 123.456 | With prefix and suffix whitespace, positive sign |
" \t\r\n\f\v-123.456 \t\r\n\f\v" | -123.456 | With prefix and suffix whitespace, negative sign |
" \t\r\n\f\v+1.234e5 \t\r\n\f\v" | 123400 | Scientific notation |
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v" | 123400 | Scientific notation with positive exponent |
" \t\r\n\f\v+1.23456e-1 \t\r\n\f\v" | 0.123456 | Scientific notation with negative exponent |
"Infinity" | Infinity | |
"NaN" | NaN | |
"123.456a" | Error | Invalid format |
"1.7e409" | Infinity | Overflow |
"-1.7e409" | -Infinity | Overflow |
Non-strict modeβ
Always returns nullable type.
BNF definitionβ
<float> ::= <whitespace>* <value> <whitespace>*
<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
<value> ::= <decimal> | <infinity> | <nan>
<decimal> ::= <sign>? <significand> <exponent>?
<infinity> ::= <sign>? <inf_literal>
<nan> ::= <sign>? <nan_literal>
<sign> ::= "+" | "-"
<significand> ::= <digits> | <digits> "." <digits> | <digits> "." | "." <digits>
<digits> ::= <digit>+
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<exponent> ::= <e_marker> <sign>? <digits>
<e_marker> ::= "e" | "E"
<inf_literal> ::= <"INF" case-insensitive> | <"INFINITY" case-insensitive>
<nan_literal> ::= <"NAN" case-insensitive>
Rule descriptionβ
-
Supports all valid formats from strict mode;
-
Invalid format converts to NULL;
-
Overflow converts to +|-Infinity.
Examplesβ
String | float/double | Comment |
---|---|---|
"123.456" | 123.456 | |
"12345." | 12345 | |
".123456" | 0.123456 | |
" \t\r\n\f\v123.456 \t\r\n\f\v" | 123.456 | With prefix and suffix whitespace |
" \t\r\n\f\v+123.456 \t\r\n\f\v" | 123.456 | With prefix and suffix whitespace, positive sign |
" \t\r\n\f\v-123.456 \t\r\n\f\v" | -123.456 | With prefix and suffix whitespace, negative sign |
" \t\r\n\f\v+1.234e5 \t\r\n\f\v" | 123400 | Scientific notation |
"Infinity" | Infinity | |
"NaN" | NaN | |
"123.456a" | NULL | Invalid format |
"1.7e409" | Infinity | Overflow |
"-1.7e409" | -Infinity | Overflow |
From boolβ
true converts to 1, false converts to 0.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
From integerβ
Follows C++ static cast semantics. May lose precision.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
From float to doubleβ
Follows C++ static cast semantics.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
From double to floatβ
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Rule descriptionβ
-
Follows C++ static cast semantics.
-
Overflow converts to +-Infinity.
Examplesβ
double | float | Comment |
---|---|---|
1.79769e+308 | Infinity | Overflow |
-1.79769e+308 | -Infinity | Overflow |
From decimal to floatβ
Casting Decimal type to float may lose precision.
Doris's Decimal(p, s)
type is actually represented by an integer in memory, where the integer value equals Decimal actual value * 10^s
. For example, a Decimal(10, 6)
value 1234.56789
is represented by integer value 1234567890
in memory.
When converting Decimal type to float or double type, Doris actually performs the following operation: static_cast<float>(integer value in memory) / (10^scale)
.
Strict modeβ
Converts to Infinity if overflow.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
Decimal(76, 6) | float | Comment |
---|---|---|
123456789.012345 | 123456790 | Casting to float will lose precision |
9999999999999999999999999999999999999999999999999999999999999999999999.123456 | Infinity | Overflow |
Non-strict modeβ
Converts to Infinity if overflow.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
Decimal(76, 6) | float | Comment |
---|---|---|
123456789.012345 | 123456790 | Casting to float will lose precision |
9999999999999999999999999999999999999999999999999999999999999999999999.123456 | Infinity | Overflow |
From decimal to doubleβ
Currently, Decimal type can have at most 76 significant digits. Casting to double type does not have overflow issues, only precision loss issues.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
Decimal(76, 6) | double | Comment |
---|---|---|
123456789.012345 | 123456789.012345 | 15 significant digits, casting to double will not lose precision |
12345678901.012345 | 12345678901.012344 | 17 significant digits, casting to double will lose precision |
9999999999999999999999999999999999999999999999999999999999999999999999.123456 | 1e+70 | Will lose precision |
From date to floatβ
Strict modeβ
Return error.
Non-strict modeβ
Concatenates the year, month, and day numbers of the date in order to form an integer, with month and day treated as two digits, padding with a leading 0 if less than 10. Then static_cast this integer to float, which may lose precision.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
date | float | Comment |
---|---|---|
2025-04-21 | 20250420 | Precision loss |
From date to doubleβ
Strict modeβ
Return error.
Non-strict modeβ
Concatenates the year, month, and day numbers of the date in order to form an integer, with month and day treated as two digits, padding with a leading 0 if less than 10. Then static_cast this integer to double.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
date | double | Comment |
---|---|---|
2025-04-21 | 20250421 | 8 significant digits, no precision loss |
From datetime to floatβ
Strict modeβ
Return error.
Non-strict modeβ
Discards the microsecond part of datetime, then concatenates the year, month, day, hour, minute, and second in order to form an integer, with month, day, hour, minute, and second treated as two digits, padding with a leading 0 if less than 10. Then static_cast this integer to float, which may lose precision.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
datetime | float | Comment |
---|---|---|
2025-03-14 17:00:01.123456 | 20250314170001 | Precision loss |
9999-12-31 23:59:59.999999 | 99991231235959 | Precision loss |
From datetime to doubleβ
Strict modeβ
Return error.
Non-strict modeβ
Discards the microsecond part of datetime, then concatenates the year, month, day, hour, minute, and second in order to form an integer, with month, day, hour, minute, and second treated as two digits, padding with a leading 0 if less than 10. Then static_cast this integer to double.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
datetime | double | Comment |
---|---|---|
2025-03-14 17:00:01.123456 | 20250314170001 | 14 significant digits, no precision loss |
9999-12-31 23:59:59.999999 | 99991231235959 |
From timeβ
Strict modeβ
Return error.
Non-strict modeβ
Converts to float/double number in microseconds.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
Time | float | Comment |
---|---|---|
00:00:01 | 1000000 | |
838:59:58 | 3020398000000 | |
838:59:58.123456 | 3020398123456 |
From other typesβ
Not supported.