Basic module

formatFloat

procedure · 1 overloadSource

Wraps a float for print so it comes out with a chosen width, number of decimals or notation.

formatFloat :: (
    value: Any,
    width := -1,
    trailing_width := -1,
    mode := FormatFloat.Mode.DECIMAL,
    zero_removal := FormatFloat.Zero_Removal.YES
) -> FormatFloat

Parameters

value: Any
width := -1
the least number of characters; shorter text is padded with spaces on the left. -1 for no padding.
trailing_width := -1
digits after the decimal point; 6 when negative.
mode := FormatFloat.Mode.DECIMAL
.DECIMAL, .SCIENTIFIC or .SHORTEST.
zero_removal := FormatFloat.Zero_Removal.YES
whether zeros at the end of the fraction are dropped: .YES drops them all, .ONE_ZERO_AFTER_DECIMAL keeps one, .NO keeps them. With trailing_width set, dropped zeros leave spaces so columns still line up.

Returns

ExampleOpen in playground ↗
assert(tprint("%", formatFloat(3.14159, trailing_width = 2)) == "3.14");

See also