Osy#the first language built for agents
Agents firstAgentic appsWorkflowsDurable Execution — built inSecurityTestingThe editorThe UI modelOne program

Reference / Function

Text.ByteSize

Text.ByteSize(bytes) → string (a byte count as words — "2.7 MB")

Formats a byte count as the words a person reads — "0 B", "1.5 KB", "2.7 MB". Binary units (1024) with the conventional KB/MB/GB labels. An ordinary function, so the same call formats a size in a grid cell, a detail line or a tooltip.

stable1 example compiled by CIfunctiontextstdlibformat

Summary#

A file's size is stored as a number of bytes and read by a person as words. Text.ByteSize converts one to the other:

string SizeLabel(int bytes) {
  return Text.ByteSize(bytes);     // 2831155 → "2.7 MB"
}

Signature#

string Text.ByteSize(<int> bytes)

Description#

Units are binary (1024 per step) with the conventional labels — B, KB, MB, GB, TB, PB — which is what du -h, docker and node print, and what a developer reading a file size expects.

Whole bytes read as an integer; everything above carries one decimal:

bytesreads as
00 B
999999 B
10241.0 KB
15361.5 KB
28311552.7 MB

A negative count is clamped to 0 B — a negative size is not a thing, and -5 B would only ever be a bug showing through. The answer is identical on the server and in the browser, so the same expression is safe wherever it runs.

Why a function, not a column setting#

A size is not only ever shown in a grid. The same string belongs in a detail line, a tooltip, a confirmation message — so formatting lives in a function you call, not in a format flag on some control's contract. A grid renders one with an ordinary cell template:

Grid(rows: files, columns: [ new GridColumn { Key = "Size", Label = "Size", Align = "right" } ]) { f =>
  slot Size { f => Text(Text.ByteSize(f.Size)); }
}

The alternative — a Format = "bytes" marker on the column — would have to be re-invented on every surface that ever shows a size, and the next format after it (durations, percentages, compact counts) would each need their own marker. A function composes; a marker is a catalogue.

See also#

Related

Convert

Explicit conversion between types — number to text, text to number, a Guid to its text form. Osy# will not convert…

control — foreign UI controls (charts, grids, maps)

A `control` block declares the contract of a foreign UI widget — a chart, a data grid, a map — that a small JavaScript…