Skip to main content

How to Write a Number in YAML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Numbers in YAML need no quotes: gears: 7 is a whole number and price: 449.99 a decimal. A few forms surprise people, though, because PyYAML follows the older YAML 1.1 rules: a leading zero makes a number octal, and 1e3 without a dot stays text. This guide writes numbers and checks each one with Python.

Syntax​

Syntax
<key>: <whole number>
<key>: <decimal>
<key>: <digits>.<digits>e+<exponent>
  • Whole numbers: digits, with an optional - in front, such as 7 or -3.
  • Decimals: a dot between the digits, such as 449.99.
  • Scientific notation: in PyYAML, the number needs a dot before the e: 1.5e+3.

PyYAML calls itself "a complete YAML 1.1 parser" on its project page. The YAML 1.1 float type requires a dot in its base-10 form: [-+]?([0-9][0-9_]*)?\.[0-9.]*([eE][-+][0-9]+)?.

The Example​

numbers.yaml holds a bike's number of gears, its price and a budget written in scientific notation.

Prerequisites​

  • uv and Python, with PyYAML in a project (uv add pyyaml), to check the file. This guide uses PyYAML 6.0.4 on Python 3.15.2.

Step 1: Write the File​

numbers.yaml
gears: 7
price: 449.99
budget: 1.5e+3

numbers.yaml in a macOS-style code window: gears: 7, price: 449.99 and budget: 1.5e+3.

Step 2: Check It with PyYAML​

Save this checker as show.py. It loads any YAML file you give it and prints the result:

show.py
import sys
from pprint import pp

import yaml

with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
Terminal
uv run python show.py numbers.yaml
Output
{'gears': 7, 'price': 449.99, 'budget': 1500.0}

The terminal after uv run python show.py numbers.yaml: gears 7, price 449.99 and budget 1500.0.

7 became an integer, 449.99 a decimal, and 1.5e+3 the decimal 1500.0. The command works unchanged in PowerShell.

Three Traps​

number-traps.yaml
exponent: 1e3
leading_zero: 017
hexadecimal: 0x1F
Output
{'exponent': '1e3', 'leading_zero': 15, 'hexadecimal': 31}
  • 1e3 stayed the text '1e3': without a dot, it doesn't match the YAML 1.1 number forms.
  • 017 became 15: the YAML 1.1 integer type says integers may "have leading “0” to signal an octal base".
  • 0x1F became 31, read as hexadecimal.

YAML 1.2 changed the first two: its core schema writes octal as 0o17 and accepts 1e3 as a number. A file can therefore load differently in PyYAML and in a YAML 1.2 tool.

Common Mistakes​

MistakeWhat PyYAML didDo this instead
A code with a leading zero, such as a postcode, written as a number: 017Read it as octal and returned 15Quote it: postcode: "017" loaded as '017'
Scientific notation without a dot: 1e3Returned the text '1e3'Write 1.0e+3, or 1000

Troubleshooting​

SymptomLikely causeFix
A number arrives as textIt's in a form YAML doesn't read as a number, such as 1e3Write it with a dot, or as plain digits
A code comes out as a different numberA leading zero made it octalQuote it

FAQ​

How do I keep a number as text?​

Put it in quotes. postcode: "017" and phone: "0113 496 0000" both loaded as text. Without quotes, the result depends on the digits: 017 became 15, but 01134960000, which has a 9 in it and so can't be octal, stayed the text '01134960000'. Quotes make it text every time.

Can I use underscores in big numbers?​

The YAML 1.1 integer form allows them, as in its own example, +685_230.

Conclusion​

gears: 7, price: 449.99 and budget: 1.5e+3 loaded as numbers, while 1e3 stayed text and 017 turned into 15. Quote anything that's a code rather than an amount.

Additional Resources​