Forge

std.fmt

Formatting utilities for converting values to strings.

Functions

format_i32

def format_i32(buf: ptr[char], value: int32) -> uint64

Format a signed 32-bit integer as a decimal string into buf. Writes a null terminator. Returns the number of characters written (excluding the null terminator).

from std.fmt import format_i32
from std.alloc import alloc
from std.io import puts

pub def main() -> int32:
    let buf: ptr[char] = alloc(16)
    let len = format_i32(buf, -42)
    # buf == "-42\0", len == 3
    puts(buf)
    free(buf)
    return 0

format_f64

def format_f64(buf: ptr[char], value: float64) -> uint64

Format a 64-bit float as a decimal string into buf. Writes a null terminator. Returns the number of characters written. x86_64 only.

let len = format_f64(buf, 3.14)

Buffer Requirements

Implementation Notes

Planned Extensions

format_u32, format_i64, format_u64, format_ptr, and sprintf-style formatting are future work.

Target Support

All targets: pure software, no syscalls needed (format_f64 is x86_64 only).