quantity

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package quantity parses whole numbers written with a scale, the way budgets are typed on a command line: "300k", "1.5M", "2Mi", "3e5". SI prefixes, IEC binary prefixes and scientific exponents are accepted, and the scale is applied with exact integer arithmetic, so "1.1k" is exactly 1100 and never 1100.0000000000002.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSyntax reports a spelling that is not a number followed by one known
	// scale: a bad shape, an unknown suffix, or a suffix and an exponent at
	// the same time.
	ErrSyntax = errors.New("invalid quantity syntax")
	// ErrFraction reports a spelling whose value is not a whole number, such
	// as "1.2345k" (1234.5) or a bare "1.5".
	ErrFraction = errors.New("quantity is not a whole number")
	// ErrRange reports a value that does not fit in an int.
	ErrRange = errors.New("quantity out of range")
)

Functions

This section is empty.

Types

type Quantity

type Quantity int

Quantity is a whole number parsed from a scaled spelling.

func Parse

func Parse(s string) (Quantity, error)

Parse reads a whole number with an optional scale. The accepted forms are a plain integer ("300000"), an SI prefix ("300k", "1.5M"), an IEC binary prefix ("2Mi") or a scientific exponent ("3e5"). A fractional mantissa is allowed when the result is a whole number; a suffix and an exponent cannot be combined.

func (Quantity) Int

func (q Quantity) Int() int

Int returns the number as a plain int.

Jump to

Keyboard shortcuts

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