Skip to main content

Convert to float/double

From string​

Behavior Change

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​

Stringfloat/doubleComment
"123.456"123.456
"123456."123456
"123456"123456
".123456"0.123456
" \t\r\n\f\v123.456 \t\r\n\f\v"123.456With prefix and suffix whitespace
" \t\r\n\f\v+123.456 \t\r\n\f\v"123.456With prefix and suffix whitespace, positive sign
" \t\r\n\f\v-123.456 \t\r\n\f\v"-123.456With prefix and suffix whitespace, negative sign
" \t\r\n\f\v+1.234e5 \t\r\n\f\v"123400Scientific notation
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v"123400Scientific notation with positive exponent
" \t\r\n\f\v+1.23456e-1 \t\r\n\f\v"0.123456Scientific notation with negative exponent
"Infinity"Infinity
"NaN"NaN
"123.456a"ErrorInvalid format
"1.7e409"InfinityOverflow
"-1.7e409"-InfinityOverflow

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​

Stringfloat/doubleComment
"123.456"123.456
"12345."12345
".123456"0.123456
" \t\r\n\f\v123.456 \t\r\n\f\v"123.456With prefix and suffix whitespace
" \t\r\n\f\v+123.456 \t\r\n\f\v"123.456With prefix and suffix whitespace, positive sign
" \t\r\n\f\v-123.456 \t\r\n\f\v"-123.456With prefix and suffix whitespace, negative sign
" \t\r\n\f\v+1.234e5 \t\r\n\f\v"123400Scientific notation
"Infinity"Infinity
"NaN"NaN
"123.456a"NULLInvalid format
"1.7e409"InfinityOverflow
"-1.7e409"-InfinityOverflow

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​

doublefloatComment
1.79769e+308InfinityOverflow
-1.79769e+308-InfinityOverflow

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)floatComment
123456789.012345123456790Casting to float will lose precision
9999999999999999999999999999999999999999999999999999999999999999999999.123456InfinityOverflow

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)floatComment
123456789.012345123456790Casting to float will lose precision
9999999999999999999999999999999999999999999999999999999999999999999999.123456InfinityOverflow

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)doubleComment
123456789.012345123456789.01234515 significant digits, casting to double will not lose precision
12345678901.01234512345678901.01234417 significant digits, casting to double will lose precision
9999999999999999999999999999999999999999999999999999999999999999999999.1234561e+70Will 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​

datefloatComment
2025-04-2120250420Precision 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​

datedoubleComment
2025-04-21202504218 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​

datetimefloatComment
2025-03-14 17:00:01.12345620250314170001Precision loss
9999-12-31 23:59:59.99999999991231235959Precision 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​

datetimedoubleComment
2025-03-14 17:00:01.1234562025031417000114 significant digits, no precision loss
9999-12-31 23:59:59.99999999991231235959

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​

TimefloatComment
00:00:011000000
838:59:583020398000000
838:59:58.1234563020398123456

From other types​

Not supported.