BudiBadu Logo
Samplebadu

YAML by Example: Numeric Types

1.2

Numbers in different formats. Integers, floats, and special notations.

Code

# Regular integers and floats
age: 30
price: 19.99
negative: -42

# Scientific notation
avogadro: 6.022e23
planck: 6.626e-34

# Different number bases
hex_color: 0xFF5733    # Hexadecimal = 16733047
octal_perms: 0o755     # Octal = 493
binary_flags: 0b1010   # Binary = 10

# Large numbers with underscores for readability
population: 7_900_000_000
big_number: 1_000_000.50

# Special float values
infinity: .inf
not_a_number: .nan

Explanation

YAML automatically detects numeric types. Regular numbers work as expected, with decimals creating floats. Scientific notation uses e for the exponent. Hexadecimal numbers start with 0x, octal with 0o, and binary with 0b.

Underscores can make large numbers more readable and are ignored by the parser. YAML also supports special float values like infinity and NaN (not a number), which can be useful for mathematical computations or representing undefined numeric states.

Code Breakdown

6-7
Scientific notation for very large or small numbers.
10-12
Different bases are prefixed: 0x for hex, 0o for octal, 0b for binary.