Standard library · Basics

fmt

Turning numbers and booleans into text, aligning text, and building a string from a template.

import fmt
On this page
  1. Functions

When you want tidy, readable output, the fmt package is what you reach for. With it you turn numbers into text, line text up in fixed-width columns, and build sentences from a template.

The templates are simple: every {} in the template is filled with the next element of the vector of values. Since the values are strings, convert numbers first with a function such as Int.

Functions

Int

func Int(i: i64): str
Turns a whole number into text, with a minus sign where one is needed.
fmt-Int.salam
import fmt

func main:
    println "[" + fmt.Int(-42) + "]"
    println "[" + fmt.Uint(42 as u64) + "]"
    println "[" + fmt.Float(3.25) + "]"
    println "[" + fmt.FloatPrec(3.14159, 2) + "]"
    println "[" + fmt.Bool(true) + "]"
    println "[" + fmt.Hex(255 as u64) + "]"
end
Output[-42] [42] [3.25] [3.14] [true] [ff]

Float

func Float(f: f64): str
Turns a decimal number into text and leaves off the useless trailing digits.

The example for this item is shown with the ones above: fmt-Int.salam

Bool

func Bool(b: bool): str
Turns a boolean into "true" or "false".

The example for this item is shown with the ones above: fmt-Int.salam

PadLeft

func PadLeft(s: str, width: int): str
If the text is shorter than width, adds spaces on its left until it reaches that width. The width counts characters, so it behaves correctly outside ASCII too. In a column of numbers this is what lines them up on the right.
fmt-PadLeft.salam
import fmt

func main:
    println "[" + fmt.PadLeft("42", 6) + "]"
    println "[" + fmt.PadRight("42", 6) + "]"
    println "[" + fmt.Center("42", 6) + "]"
    println "[" + fmt.PadLeft("salam", 10) + "]"
end
Output[ 42] [42 ] [ 42 ] [ salam]

PadRight

func PadRight(s: str, width: int): str
Like PadLeft, but puts the spaces on the right of the text.

The example for this item is shown with the ones above: fmt-PadLeft.salam

Center

func Center(s: str, width: int): str
Places the text in the middle of the given width. When the padding does not divide evenly, the extra space goes on the right.

The example for this item is shown with the ones above: fmt-PadLeft.salam

Sprintf

func Sprintf(tmpl: str, args: Vector<str>): str
Takes the template and fills each {} in turn from the args vector. If the values run out the remaining holes are left empty, and extra values are ignored.
fmt-Sprintf.salam
import fmt

func main:
    mut values := Vector {} as Vector<str>
    defer values.free()
    values.push("Sara")
    values.push(fmt.Int(19))
    println fmt.Sprintf("{} scored {}.", values)
end
OutputSara scored 19.

Fprintf

func Fprintf(f: File*, tmpl: str, args: Vector<str>): int
Like Sprintf, but writes the result straight into an open file and returns the number of bytes written.
fmt-Fprintf.salam
import fmt
import io
import os

func main:
    mut values := Vector {} as Vector<str>
    defer values.free()
    values.push("25")
    f := os.Open("temperature.txt", "wb")
    fmt.Fprintf(f, "today it is {} degrees\n", values)
    io.Close(f)
    print io.ReadFile("temperature.txt")
end
Outputtoday it is 25 degrees

FloatPrec

func FloatPrec(f: f64, prec: int): str
Turns a decimal number into text with the number of decimal places you ask for, rounding it.

The example for this item is shown with the ones above: fmt-Int.salam

Uint

func Uint(u: u64): str
Turns an unsigned 64-bit number into text, for numbers too large to fit in an i64.

The example for this item is shown with the ones above: fmt-Int.salam

Hex

func Hex(u: u64): str
Writes the number in base sixteen in lower case, with no 0x prefix.

The example for this item is shown with the ones above: fmt-Int.salam