Especificadores de Formato
En esta página
Dentro de la interpolación {} puedes añadir :spec para controlar el formato
del valor. Los especificadores soportados son:
| Spec | Efecto | Ejemplo |
|---|---|---|
:.Nf |
N decimales | {pi:.2f} → 3.14 |
:0Nd |
entero con N dígitos relleno de ceros | {n:04d} → 0042 |
:#x |
entero en hexadecimal con prefijo 0x |
{255:#x} → 0xff |
Precisión de float, zero-padding y hex en interpolación.
04-format-specs.zolo
// Feature: Format specs in interpolation
// Syntax: `"{expr:spec}"` — spec after `:`
// When to use: float precision, zero-padding, numeric bases.
// -- Float precision (`.Nf`) ------------------------------------
let pi = 3.14159265
print("{pi:.2f}") // 3.14
print("{pi:.4f}") // 3.1416
print("{pi:.0f}") // 3
// -- Numeric zero-padding (`0Nd`) -------------------------------
let n = 42
print("{n:04d}") // 0042
print("{n:08d}") // 00000042
// -- Hex with prefix (`#x`) ------------------------------------
print("{255:#x}") // 0xff
print("{16:#x}") // 0x10
// -- Practical combinations ------------------------------------
let price = 9.5
print("$ {price:.2f}") // $ 9.50
let id = 7
print("ID: {id:04d}") // ID: 0007
// Coordinates with 3 decimal places.
let x = 1.234567
let y = -0.5
print("({x:.3f}, {y:.3f})")
// expected: (1.235, -0.500)
// Sequential numeric IDs with zero-pad.
var i = 1
while i <= 3 {
print("user_{i:03d}")
i = i + 1
}
// expected:
// user_001
// user_002
// user_003
// NOTE: alignment (`>` and `<`) and binary/octal bases are not yet
// supported in the current interpolator. Use precision and zero-pad
// for the most common cases.
Para una cadena de formato dinámica o varios valores, importa std::string y
usa string.format. El formato al estilo printf acepta enteros, cadenas,
precisión de float, hexadecimal, ancho, flags y un %% literal.
Construye cadenas formateadas con %d, %s, %f, %x, ancho, flags y %%.
10-string-format.zolo
// Feature: `string.format(fmt, args...)` — printf/Lua-style formatting
// Syntax: `string.format("%d %s %.2f", n, s, x)` (after `use std::string`)
// When to use: building formatted strings with width / precision / flags,
// e.g. fixed-decimal floats, zero-padded ints, hex.
//
// Works on every backend: VM, native (Cranelift), and LLVM — all routed
// through the `std::string::format` runtime formatter, so the output is
// identical across engines.
use std::string
// -- Integers (`%d` / `%i`) ------------------------------------
print(string.format("%d", 42)) // 42
print(string.format("%d and %d", 1, 2)) // 1 and 2
// -- Strings (`%s`, via tostring) ------------------------------
print(string.format("hello, %s!", "world")) // hello, world!
print(string.format("%s = %d", "x", 7)) // x = 7
// -- Floats with precision (`%.Nf`) ----------------------------
print(string.format("%.3f", 1.5)) // 1.500
print(string.format("%.0f", 42.0)) // 42
print(string.format("%f", 3.14)) // 3.140000 (bare %f → 6 decimals)
// -- Hex (`%x` / `%X`) -----------------------------------------
print(string.format("%x", 255)) // ff
print(string.format("%X", 255)) // FF
// -- Width, zero-pad and flags ---------------------------------
print(string.format("%05d", 42)) // 00042
print(string.format("%-5d|", 7)) // 7 |
print(string.format("%+d", 9)) // +9
// -- Literal percent (`%%`) ------------------------------------
print(string.format("100%% done")) // 100% done
// -- Practical: an auto-scaled duration formatter --------------
fn fmt_ms(ms: float) -> str {
let secs = ms / 1000.0
if ms >= 1000.0 {
return string.format("%.3fs", secs)
} else {
return string.format("%.3fms", ms)
}
}
print(fmt_ms(1500.0)) // 1.500s
print(fmt_ms(12.5)) // 12.500ms
// expected:
// 42
// 1 and 2
// hello, world!
// x = 7
// 1.500
// 42
// 3.140000
// ff
// FF
// 00042
// 7 |
// +9
// 100% done
// 1.500s
// 12.500ms
Nota: la alineación (
>,<) y las bases octal/binario no están disponibles en el interpolador actual. Usa precisión y zero-padding para los casos más comunes.
Consulta también