Numeric Values in YAML to ENV Conversion
Learn how YAML numeric types including integers, floats, octal, hexadecimal, and special values like infinity are handled when converting to ENV string format.
Detailed Explanation
YAML supports a rich set of numeric formats that all become plain strings in ENV files. Understanding these conversions is essential to avoid data loss or misinterpretation.
YAML numeric types:
# Standard integers and floats
port: 8080
timeout: 30.5
negative: -1
zero: 0
# Octal (common for file permissions)
file_mode_v1: 0755 # YAML 1.1 octal = decimal 493
file_mode_v2: 0o755 # YAML 1.2 octal = decimal 493
# Hexadecimal
max_memory: 0xFF # decimal 255
# Scientific notation
threshold: 1.5e-3 # 0.0015
# Special values
max_connections: .inf # infinity
undefined_ratio: .nan # not a number
Converted to ENV:
PORT=8080
TIMEOUT=30.5
NEGATIVE=-1
ZERO=0
FILE_MODE_V1=493
FILE_MODE_V2=493
MAX_MEMORY=255
THRESHOLD=0.0015
MAX_CONNECTIONS=Infinity
UNDEFINED_RATIO=NaN
Critical conversion issues:
Octal number expansion. YAML interprets
0755as octal and converts it to decimal 493. If you intended the literal string"0755"(for example, a file permission argument forchmod), you must quote it in YAML:file_mode: "0755". In the ENV output, this becomesFILE_MODE=0755.Hexadecimal conversion.
0xFFbecomes255. The hex representation is lost. If you need the hex string, quote it:"0xFF".Infinity and NaN. These YAML special values have no standard ENV representation. They typically become the strings
InfinityandNaN, which must be parsed explicitly by the consuming application.Scientific notation.
1.5e-3may be expanded to0.0015or kept as1.5e-3depending on the converter. Both are valid number representations.Leading zeros. A value like
007in YAML 1.1 is octal (decimal 7), not the string"007". In YAML 1.2,007remains the integer 7 (not octal). Always quote if you need the literal form.
Best practice: When the string representation matters more than the numeric value (file permissions, zero-padded IDs, hex colors), always quote the value in YAML before conversion to ENV.
Use Case
Converting a YAML configuration containing file permissions (0755, 0644), memory limits in hex notation, and scientific notation thresholds to ENV format for a containerized application startup script.