unitconv

package
v1.106.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: BSD-3-Clause Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package unitconv implements string conversion functionality for unit prefixes such as those from the SI and IEC standards. It is catered towards printing of values for human consumption. Consequently it prints of reasonable precision proportional to the magnitude of the value itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats the duration with precision proportional to the magnitude of the duration. This differs from the time.Duration.String format in that it additionally supports year ('y') and day ('d') units, where a year is exactly 365.25 days and a day is exactly 24 hours.

Examples:

12ns
5.12μs
832μs
8.14ms
512ms
1.3s
58s
5m23s
4h23m
4d16h
17y53d

func FormatIEC

func FormatIEC[N numeric](v N, unitSuffix string) string

FormatIEC formats the number using a binary prefix (e.g. Ki, Mi, Gi, etc.) combined with the provided unit suffix. Any leading spaces in the unitSuffix are moved before the metric prefix.

Example:

FormatIEC(123, "B")         => "123B"
FormatIEC(12345678, " B/s") => "11.77 MiB/s"

Exact precision and width of the output may change in the future.

func FormatSI

func FormatSI[N numeric](v N, unitSuffix string) string

FormatSI formats the number using a metric prefix (e.g. k, M, G, etc.) combined with the provided unit suffix. Any leading spaces in the unitSuffix are moved before the metric prefix.

Example:

FormatSI(123, "P/s")     => "123P/s"
FormatSI(12345678, " W") => "12.3 MW"

Exact precision and width of the output may change in the future.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL