Berserk Docs
Scalar FunctionsString Functions

format_bytes

Formats a size in bytes as text, such as `19.08 MB`.

Units step by 1024 through Bytes, KB, MB, GB, TB, PB and EB. Without units, the largest unit that keeps the value at least 1 is chosen; negative sizes stay in Bytes. The value is rounded half away from zero to the precision and printed without trailing zeros.

Syntax

format_bytes(size, precision, units)

Parameters

Prop

Type

Given a dynamic field

This parameter is string-only. In permissive mode — the default — almost every field arrives as a dynamic, so passing one works because Berserk injects asstring: extract-or-null, the value when it really is a string and null otherwise, so the call yields null (a predicate yields false). A property bag or array is therefore not matched, and tostring() is never applied implicitly — use it explicitly to match a bag's JSON text, keys included. Strict mode rejects the dynamic instead of coercing it. See String coercion and the asXXX family.

Returns: string

Examples

Example 1

print format_bytes(564), format_bytes(20010332), format_bytes(20010332, 2)
print_0 (string)print_1 (string)print_2 (string)
564 Bytes19 MB19.08 MB

Example 2

print format_bytes(20010332, 0, 'KB')
print_0 (string)
19541 KB

On this page