Skip to main content
The format_bytes function formats a numeric value as a human-readable string representing data size in bytes with appropriate units (KB, MB, GB, etc.). Use this function to make byte values more readable in reports, dashboards, and log analysis.

For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.
In Splunk SPL, you typically need custom eval expressions or lookup tables to format bytes. APL’s format_bytes provides this functionality natively.
| eval size_str=if(bytes<1024, bytes." B", if(bytes<1048576, round(bytes/1024,2)." KB", round(bytes/1048576,2)." MB"))
In ANSI SQL, formatting bytes requires complex CASE statements. APL’s format_bytes simplifies this operation.
SELECT CASE 
  WHEN bytes < 1024 THEN CONCAT(bytes, ' B')
  WHEN bytes < 1048576 THEN CONCAT(ROUND(bytes/1024, 2), ' KB')
  ELSE CONCAT(ROUND(bytes/1048576, 2), ' MB')
END AS size_str FROM logs;

Usage

Syntax

format_bytes(value, precision, units, base)

Parameters

NameTypeRequiredDescription
valuenumberYesThe numeric value representing bytes to format.
precisionnumberNoNumber of decimal places (default: 0).
unitsstringNoTarget units (e.g., ‘KB’, ‘MB’, ‘GB’). If omitted, units are auto-selected.
basenumberNoEither 2 (default, 1024-based) or 10 (1000-based) for unit calculations.

Returns

Returns a formatted string representing the byte value with appropriate units.

Use case examples

  • Log analysis
  • OpenTelemetry traces
  • Security logs
Format request duration values as human-readable sizes for better analysis of payload patterns.Query
['sample-http-logs']
| extend formatted_duration = format_bytes(req_duration_ms, 2)
| summarize avg_size = avg(req_duration_ms), formatted_avg = format_bytes(toint(avg(req_duration_ms)), 2) by status
| sort by avg_size desc
| limit 10
Run in PlaygroundOutput
statusavg_sizeformatted_avg
50087654328.36 MB
20034567893.30 MB
40412345671.18 MB
301456789446.08 KB
This query formats average request duration values by HTTP status code, making it easier to identify which status codes are associated with larger data transfers.
  • parse_bytes: Parses a formatted byte string back to a numeric value. Use this to reverse the formatting operation.
  • strlen: Returns the length of a string in characters. Use this when you need character count rather than byte formatting.