std / strings/conv

strings/conv

import "std:strings/conv"

Provides conversions between string representations and numeric types. Parsing functions convert strings to numbers. Formatting functions convert numbers to strings.

View source on Codeberg →

Types

#
type Error int

Error represents an error code returned by parsing and formatting functions.

Error Constants

#
const (
    OK       Error = iota
    ErrSyntax
    ErrRange
)

Error constants for conversion operations. OK indicates success. ErrSyntax indicates the input has invalid syntax. ErrRange indicates the value is out of range for the target type.

Parsing - Integer

#
func Atoi(s string) (int64, Error)

Converts a decimal string to an int. Equivalent to ParseInt(s, 10). Returns ErrSyntax if the string is not a valid integer, or ErrRange if the value overflows.

#
func ParseInt(s string, base int) (int64, Error)

Parses a signed integer string in the given base (2 to 36). Returns the parsed value and an error code.

Parsing - Unsigned

#
func ParseUint(s string, base int) (uint64, Error)

Parses an unsigned integer string in the given base (2 to 36). Returns the parsed value and an error code.

Parsing - Float

#
func ParseFloat(s string) (float64, Error)

Parses a floating-point number from a string. Accepts decimal notation and optional exponent. Returns ErrSyntax if the string is not a valid float.

Parsing - Bool

#
func ParseBool(s string) (bool, Error)

Parses a boolean value from a string. Accepts "true", "TRUE", "True", "false", "FALSE", "False", "1", "0". Returns ErrSyntax for unrecognized values.

Formatting - Integer

#
func Itoa(i int64) string

Converts an int to its decimal string representation. Equivalent to FormatInt(i, 10).

#
func FormatInt(i int64, base int) string

Formats a signed integer in the given base (2 to 36) and returns the resulting string.

Formatting - Unsigned

#
func FormatUint(u uint64, base int) string

Formats an unsigned integer in the given base (2 to 36) and returns the resulting string.

Formatting - Float

#
func FormatFloat(f float64, prec int) string

Formats a floating-point number as a decimal string with the given number of digits after the decimal point.

Formatting - Bool

#
func FormatBool(b bool) string

Returns "true" or "false" according to the value of b.