YAML supports various number formats, making it versatile for representing numerical data. Understanding how to use numbers in YAML is crucial for creating accurate and efficient configurations.
YAML allows you to represent whole numbers (integers) in multiple ways:
age: 30
population: 7_000_000_000
hex: 0xFF
octal: 0o77
binary: 0b1010
Note the use of underscores for readability in large numbers and the prefixes for different number bases.
For decimal numbers, YAML supports standard and scientific notation:
pi: 3.14159
avogadro: 6.022e23
tiny: 1.0e-10
YAML also recognizes special number values:
positive_infinity: .inf
negative_infinity: -.inf
not_a_number: .nan
Numbers can be used in various YAML structures, including lists and dictionaries:
measurements:
- 10.5
- 20.3
- 15.7
coordinates:
x: 100
y: 200
z: 300
YAML typically infers the type of a number, but you can explicitly specify the type using tags:
implicit_integer: 42
explicit_integer: !!int 42
explicit_float: !!float 42.0
This can be particularly useful when working with YAML parsing in different programming languages.
Understanding number representation in YAML is essential for creating accurate and efficient configurations. By leveraging different number formats and following best practices, you can ensure your YAML files are both readable and precise.
For more information on related topics, check out YAML Booleans and YAML Strings.