On this page:
str
str.s
str.d
str.b
str.o
str.x
str.X
str.f
str.e
str.g
str.Align
str.Align.left
str.Align.center
str.Align.right
8.16.0.2

12.1 String Formatting🔗ℹ

function

fun str([elem, ...]) :: String

Equivalent to String.append(to_string(elem), ...), and intended to be used with @ notation (see At-Notation Using @).

> @str{1 + 2 = @(1 + 2)}

"1 + 2 = 3"

In the {} argument for str, use @ escapes with functions like str.f or repr to format non-strings values in ways other than the to_string default.

> @str{The key @repr(#'pi) is mapped to @str.f(math.pi, ~precision: 2).}

"The key #'pi is mapped to 3.14."

fun pct(n): str.f(n * 100, ~precision: 1) ++ "%"

> print(@str{Contents: @pct(0.251) apples

                       @pct(1/6) bananas

                       @pct(0.5) cherries})

Contents: 25.1% apples

          16.7% bananas

          50.0% cherries

function

fun str.s(v :: Any,

          ~mode: mode :: PrintMode = #'text,

          ~width: width :: maybe(NonnegInt) = #false,

          ~min_width: min_width :: maybe(NonnegInt) = width,

          ~max_width: max_width :: maybe(NonnegInt) = width,

          ~pad: pad :: Char = Char" ",

          ~align: align :: str.Align = #'left,

          ~clip_align: clip_align :: str.Align = align)

  :: String

Converts v to a string in the same way as to_string, but may adjusts the size of the resulting string if min_width or max_width is not #false. The width argument serves only as way to provide min_width and max_width at once. If min_width and max_width are both provided, an exception is thrown if max_width is less than min_width.

> str.s("hello", ~width: 10)

"hello     "

> str.s("hello", ~width: 10, ~pad: Char"_", ~align: #'center)

"__hello___"

> str.s("hello", ~max_width: 2)

"he"

> str.s("hello", ~max_width: 2, ~clip_align: #'right)

"lo"

function

fun str.d(n :: Int,

          ~width: width :: maybe(NonnegInt) = #false,

          ~min_width: min_width :: maybe(NonnegInt) = width,

          ~max_width: max_width :: maybe(NonnegInt) = width,

          ~pad: pad :: Char = Char" ",

          ~align: align :: str.Align = #'left,

          ~clip_align: clip_align :: str.Align = align,

          ~minus_sign: minus_sign :: String  = "-",

          ~plus_sign: plus_sign :: String  = "",

          ~zero_sign: zero_sign :: String  = "",

          ~sign_align: sign_align :: str.Align = #'center)

  :: String

 

function

fun str.b(n :: Int, ....) :: String

 

function

fun str.o(n :: Int, ....) :: String

 

function

fun str.x(n :: Int, ....) :: String

 

function

fun str.X(n :: Int, ....) :: String

These functions all accept the same arguments, and they convert an integer to string using a specific base: decimal for str.d, binary for str.b, octal for str.o, lowercase hexadecimal for str.x, and uppercase hexadecimal for str.X.

The min_width, max_width, pad, align, and clip_align arguments are used the same as by str.s.

The minus_sign, plus_sign, and zero_sign arguments determines how a sign is shown. By default, a sign is shown only for negative n.

The sign_align argument determines where the sign string is positioned relative to digits. A #'left or #'center value places the sign on the left, and #'right on the right. A #'left or #'right value further moves the sign to before or after any padding that is added to the number to make the result min_width characters, and it preserves the sign and clips digits, instead, when clipping characters to fit into max_width.

> str.d(-10)

"-10"

> str.x(-10)

"-a"

> str.d(-10, ~width: 8)

"-10     "

> str.d(-10, ~width: 8, ~align: #'right)

"     -10"

> str.d(-10, ~width: 8, ~align: #'right, ~sign_align: #'left)

"-     10"

> str.d(-123456, ~width: 4, ~align: #'right, ~sign_align: #'left)

"-456"

> str.d(10, ~plus_sign: "+")

"+10"

function

fun str.f(n :: Real,

          ~precision: precision :: NonnegInt = 6,

          ~keep_decimal: keep_decimal :: Any = #false,

          ~width: width :: maybe(NonnegInt) = #false,

          ~min_width: min_width :: maybe(NonnegInt) = width,

          ~max_width: max_width :: maybe(NonnegInt) = width,

          ~pad: pad :: Char = Char" ",

          ~align: align :: str.Align = #'left,

          ~clip_align: clip_align :: str.Align = align,

          ~minus_sign: minus_sign :: String  = "-",

          ~plus_sign: plus_sign :: String  = "",

          ~zero_sign: zero_sign :: String  = "",

          ~sign_align: sign_align :: str.Align = #'center,

          ~decimal: decimal :: String = ".")

  :: String

 

function

fun str.e(n :: Int, ....,

          ~exponent: exponent :: String = "e") :: String

 

function

fun str.g(n :: Int, ....,

          ~exponent: exponent :: String = "e") :: String

Like str.d, but for a real number that may include a decimal point and may be written in exponential notation. The handling of optional arguments is the same as in str.d, but ~precision, ~keep_decimal, and ~decimal arguments are accepted in addition, and str.e and str.f also accept an ~exponent argument. The functions str.f, str.e, and str.g produce strings in different forms, except that they all represent #inf, #neginf, and #nan, as "inf", "-inf", and "nan", respectively; the rules below apply for other values of n.

The str.f (“f” for “floating point”) function produces a string that uses a decimal point and precision subsequent digits. If precision is 0, then the decimal point is dropped unless keep_decimal is true. The decimal string is used to represent a decimal point.

The str.e (“e” for “exponent”) function produces a string that uses exponential notation indicated with exponent. The coefficient is written with a single digit before a decimal point, and the digit is always between 1 and 9 unless n is zero; precision digits are shown after the decimal point. An exponent is shown after exponent, and a sign is included for the exponent only if it is negative.

The str.g (“g” for “general”) function produces a string like str.f or str.e, depending on a relationship between precision and the exponent expo that would be written with str.e: if the exponent is at least -4 and less than precision, then str.f is used, otherwise str.g is used. When str.g uses str.f, it adjusts precision to precision - 1 - expo. When str.g uses str.e, it adjusts precision to precision-1. Since these adjustments only work in general when precision is positive, a precision of 0 passed to str.g is treated as 1.

> str.f(123.45)

"123.450000"

> str.f(2/3, ~precision: 2)

"0.67"

> str.e(2/3, ~precision: 2)

"6.67e-1"

> str.e(123.45e10, ~precision: 2)

"1.23e12"

> str.g(2/3, ~precision: 2)

"0.67"

> str.g(123.45e10, ~precision: 2)

"1.2e12"

> str.g(123.45e10, ~decimal: ",", ~exponent: "E")

"1,2345E12"

enumeration

enum str.Align:

  left

  center

  right

Alignment for functions like str.s.