YAML Numbers and Booleans
Numbers and booleans carry meaning without quote marks. YAML detects them automatically from the way they look. That convenience saves typing and creates a few traps, so learning the exact rules keeps your data accurate.
Integers
Whole numbers appear as plain digits. A minus sign marks negatives.
age: 25
temperature: -4
population: 1400000000Other bases use a prefix. A 0x prefix means hexadecimal, and 0o means octal.
color: 0xFF
permissions: 0o755The first value equals 255. The second equals 493 in decimal.
Floating-Point Numbers
A decimal point creates a float. Scientific notation works as well.
pi: 3.14159
price: 99.99
speed: 3.0e8
tiny: 1.5e-4YAML also defines special values for infinity and not-a-number: .inf, -.inf, and .nan. Scientific programs use them, and everyday configuration rarely needs them.
Booleans
A boolean holds one of two answers: yes or no, on or off. Modern YAML recognizes true and false.
debug: true
cache_enabled: falseSwitches in settings files use booleans constantly. A key such as debug: true reads almost like English.
The Older Boolean Problem
YAML 1.1 treated many more words as booleans. The words yes, no, on, off, y, and n all converted to true or false. That rule produced a famous trap with country codes:
countries:
- IN
- NO
- USA YAML 1.1 parser reads NO as false, so Norway disappears from the list. Quote the value to stay safe: "NO".
| Word | YAML 1.1 result | YAML 1.2 result |
|---|---|---|
| true / false | Boolean | Boolean |
| yes / no | Boolean | Text |
| on / off | Boolean | Text |
Many popular libraries, including PyYAML, still follow the older rules. Stick with true and false and the answer stays identical everywhere.
How the Parser Decides
Value typed: 42 3.14 true hello "42"
| | | | |
v v v v v
Parsed as: integer float boolean string string
The parser inspects each plain value from left to right. A match with a number pattern or boolean word wins. Anything else becomes text. Quote marks always force text.
Leading Zeros and Version Numbers
Numbers with a leading zero cause confusion. Some parsers read 0755 as octal. Quote such values when they represent codes rather than quantities.
Version numbers carry another risk. The value 3.10 becomes the float 3.1, which drops the zero. Python version 3.10 turns into 3.1 and breaks build files.
python_version: "3.10"Underscores and Commas
Do not use commas inside numbers. The value 1,000 becomes text. Some parsers accept underscores like 1_000, though the rule varies by version, so avoid both for portability.
Choosing Between Number and Text
- Use a number when you plan to calculate with the value, such as a port or a count.
- Identifiers such as phone numbers, postal codes, and versions belong in quotes.
- Any on/off switch fits a boolean.
Numbers in Everyday Files
Ports, timeouts, retry counts, and sizes all appear as integers in typical configuration files. Prices and ratios use floats. Programs receive these values ready for arithmetic, so code can add, compare, and multiply without any conversion step.
Booleans control features. A setting like dark_mode: true flips an option in an application, and the same key in a test file might switch to false to compare behavior.
Test unusual values by loading the file and printing the result. The printed types reveal how the parser interpreted each line, and catching a wrong type early saves debugging time later.
