Skip to content

strconv #

Description

strconv provides functions for converting strings to numbers and numbers to strings.

Buffer formatting

write_dec and write_dec_u write a decimal integer into a caller-provided []u8 buffer. They return the number of bytes written, or -1 when the buffer is too small.

import strconv

mut buf := []u8{len: 20}
n := strconv.write_dec(-12345, mut buf)
assert n == 6
assert buf[..n].bytestr() == '-12345'

Thousands separators

format_thousands formats built-in integer and floating-point values with a thousands separator. Pass a string for the integer separator, or a Separator to configure both integer and decimal separators.

import strconv

assert strconv.format_thousands(1234567, ' ') == '1 234 567'
assert strconv.format_thousands(1234567.89, strconv.Separator{
    integer: '.'
    decimal: ','
}) == '1.234.567,89'

The numeric type is checked at compile time; unsupported types produce a compile-time error.

add_thousands_sep groups the integer part of an already formatted numeric string, preserving its sign, fraction, and exponent. It accepts the same separator options. Unlike the numeric API, the string API keeps scientific notation as supplied.

import strconv

assert strconv.add_thousands_sep('1234567.89', ',') == '1,234,567.89'
assert strconv.add_thousands_sep('1.5e+21', ',') == '1.5e+21'
assert strconv.add_thousands_sep('-12345.5', strconv.Separator{
    integer: '.'
    decimal: ','
}) == '-12.345,5'

Constants #

const single_plus_zero = u32(0x0000_0000)

f32 constants

const single_minus_zero = u32(0x8000_0000)
const single_plus_infinity = u32(0x7F80_0000)
const single_minus_infinity = u32(0xFF80_0000)
const digits = 18

f64 constants

const double_plus_zero = u64(0x0000000000000000)
const double_minus_zero = u64(0x8000000000000000)
const double_plus_infinity = u64(0x7FF0000000000000)
const double_minus_infinity = u64(0xFFF0000000000000)
const c_dpoint = `.`

char constants

const c_plus = `+`
const c_minus = `-`
const c_zero = `0`
const c_nine = `9`
const c_ten = u32(10)

fn add_thousands_sep #

fn add_thousands_sep(s string, sep SeparatorOptions) string

add_thousands_sep inserts sep as a thousands separator into the integer part of the numeric string s. An optional leading sign (- or +) is preserved, and fractional digits are kept unchanged except that their decimal separator is replaced by sep.decimal when sep is a Separator. A string sep uses that string for the integer separator and keeps . as the decimal separator.

Because it operates on an already-formatted string, it composes with any number-to-string conversion: int/i64/u64.str(), f64.str(), strconv.ftoa_*, math.big.Integer.str(), etc.

This function does not do full locale-aware formatting: it groups only the digits before the input decimal point and does not parse or round the value.

fn atof64 #

fn atof64(s string, param AtoF64Param) !f64

atof64 parses the string s, and if possible, converts it into a f64 number

fn atof_quick #

@[direct_array_access]
fn atof_quick(s string) f64

atof_quick return a f64 number from a string in a quick way

fn atoi #

fn atoi(s string) !int

atoi is equivalent to parse_int(s, 10, 0), converted to type int. It follows V scanner as much as observed.

fn atoi16 #

fn atoi16(s string) !i16

atoi16 is equivalent to atoi(s), converted to type i16. returns an i16 [-32678 .. 32767] or an error.

fn atoi32 #

fn atoi32(s string) !i32

atoi32 is equivalent to atoi(s), converted to type i32. returns an i32 [-2147483648 .. 2147483647] or an error.

fn atoi64 #

@[direct_array_access]
fn atoi64(s string) !i64

atoi64 converts radix 10 string to i64 type. returns an i64 [-9223372036854775808 .. 9223372036854775807] or an error.

fn atoi8 #

fn atoi8(s string) !i8

atoi8 is equivalent to atoi(s), converted to type i8. returns an i8 [-128 .. 127] or an error.

fn atou #

fn atou(s string) !u32

atou is equivalent to parse_uint(s, 10, 0), converted to type u32.

fn atou16 #

fn atou16(s string) !u16

atou16 is equivalent to parse_uint(s, 10, 0), converted to type u16. It returns u16 in range [0..65535] or an Error.

fn atou32 #

fn atou32(s string) !u32

atou32 is identical to atou. Here to provide a symmetrical API with atoi/atoi32 It returns u32 in range [0..4294967295] or an Error.

fn atou64 #

fn atou64(s string) !u64

atou64 is equivalent to parse_uint(s, 10, 0), converted to type u64. It returns u64 in range [0..18446744073709551615] or an Error.

fn atou8 #

fn atou8(s string) !u8

atou8 is equivalent to parse_uint(s, 10, 0), converted to type u8. It returns u8 in range [0..255] or an Error.

fn byte_to_lower #

@[inline]
fn byte_to_lower(c u8) u8

fn common_parse_int #

@[direct_array_access]
fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) !i64

common_parse_int is called by parse int and allows the parsing to stop on non or invalid digit characters and return with an error

fn common_parse_uint #

fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit bool, error_on_high_digit bool) !u64

common_parse_uint is called by parse_uint and allows the parsing to stop on non or invalid digit characters and return with an error

fn common_parse_uint2 #

@[direct_array_access]
fn common_parse_uint2(s string, _base int, _bit_size int) (u64, int)

the first returned value contains the parsed value, the second returned value contains the error code (0 = OK, >1 = index of first non-parseable character + 1, -1 = wrong base, -2 = wrong bit size, -3 = overflow)

fn dec_digits #

fn dec_digits(n u64) int

dec_digits return the number of decimal digit of an u64

fn f32_to_str #

fn f32_to_str(f f32, n_digit int) string

f32_to_str returns a string in scientific notation with max n_digit after the dot.

fn f32_to_str_l #

@[manualfree]
fn f32_to_str_l(f f32) string

Todo: Investigate precision issuesf32_to_str_l returns f as a string in decimal notation with a maximum of 8 digits after the dot.

Examples

assert strconv.f32_to_str_l(0.1234567891) == '0.12345679'
assert strconv.f32_to_str_l(34.1234567891) == '34.123455'

fn f32_to_str_l_with_dot #

@[manualfree]
fn f32_to_str_l_with_dot(f f32) string

f32_to_str_l_with_dot returns f as a string in decimal notation with a maximum of 8 digits after the dot. If the decimal digits after the dot are zero, a '.0' is appended for clarity.

Example

assert strconv.f32_to_str_l_with_dot(34.2) == '34.2'

fn f32_to_str_pad #

fn f32_to_str_pad(f f32, n_digit int) string

f32_to_str_pad returns a string in scientific notation with max n_digit after the dot.

fn f64_to_str #

fn f64_to_str(f f64, n_digit int) string

f64_to_str returns f as a string in scientific notation with max n_digit digits after the dot.

fn f64_to_str_l #

@[manualfree]
fn f64_to_str_l(f f64) string

f64_to_str_l returns f as a string in decimal notation with a maximum of 18 digits after the dot.

Example

assert strconv.f64_to_str_l(123.1234567891011121) == '123.12345678910111'

fn f64_to_str_l_with_dot #

@[manualfree]
fn f64_to_str_l_with_dot(f f64) string

f64_to_str_l_with_dot returns f as a string in decimal notation with a maximum of 18 digits after the dot. If the decimal digits after the dot are zero, a '.0' is appended for clarity.

Example

assert strconv.f64_to_str_l_with_dot(34.7) == '34.7'

fn f64_to_str_lnd1 #

@[direct_array_access; manualfree]
fn f64_to_str_lnd1(f f64, dec_digit int) string

f64_to_str_lnd1 formats a f64 to a string with dec_digit digits after the dot.

fn f64_to_str_pad #

fn f64_to_str_pad(f f64, n_digit int) string

f64_to_str returns f as a string in scientific notation with max n_digit digits after the dot.

fn format_dec_old #

@[manualfree]
fn format_dec_old(d u64, p BF_param) string

max int64 9223372036854775807

fn format_dec_sb #

@[direct_array_access]
fn format_dec_sb(d u64, p BF_param, mut res strings.Builder)

format_dec_sb formats an u64 using a strings.Builder.

fn format_es #

@[direct_array_access; manualfree]
fn format_es(f f64, p BF_param) string

format_es returns a f64 as a string formatted according to the options set in p.

fn format_fl #

@[direct_array_access; manualfree]
fn format_fl(f f64, p BF_param) string

format_fl is a strings.Builder version of format_fl.

fn format_fl_old #

@[direct_array_access; manualfree]
fn format_fl_old(f f64, p BF_param) string

strings.Builder version of format_fl

fn format_int #

@[direct_array_access; manualfree]
fn format_int(n i64, radix int) string

format_int returns the string representation of the number n in base radix for digit values > 10, this function uses the small latin leters a-z.

fn format_str #

@[manualfree]
fn format_str(s string, p BF_param) string

format_str returns the s formatted, according to the options set in p.

fn format_str_sb #

fn format_str_sb(s string, p BF_param, mut sb strings.Builder)

format_str_sb is a strings.Builder version of format_str.

fn format_thousands #

fn format_thousands[T](number T, sep SeparatorOptions) string

format_thousands returns the base-10 representation of number, inserting a thousands separator every three digits. With a Separator, its integer and decimal fields configure the output; a string uses that string for grouping and . as the decimal separator. It accepts all built-in signed and unsigned integer types, isize, usize, f32, and f64.

fn format_uint #

@[direct_array_access; manualfree]
fn format_uint(n u64, radix int) string

format_uint returns the string representation of the number n in base radix for digit values > 10, this function uses the small latin leters a-z.

fn ftoa_32 #

@[inline]
fn ftoa_32(f f32) string

ftoa_32 returns a string in scientific notation with max 8 digits after the dot.

Example

assert strconv.ftoa_32(34.1234567) == '3.4123455e+01'

fn ftoa_64 #

@[inline]
fn ftoa_64(f f64) string

ftoa_64 returns a string in scientific notation with max 17 digits after the dot.

Example

assert strconv.ftoa_64(123.1234567891011121) == '1.2312345678910111e+02'

fn ftoa_long_32 #

@[inline]
fn ftoa_long_32(f f32) string

ftoa_long_32 returns f as a string in decimal notation with a maximum of 8 digits after the dot.

Example

assert strconv.ftoa_long_32(0.1234567901) == '0.12345679'

fn ftoa_long_64 #

@[inline]
fn ftoa_long_64(f f64) string

ftoa_long_64 returns f as a string in decimal notation with a maximum of 17 digits after the dot.

Example

assert strconv.f64_to_str_l(123.1234567891011121) == '123.12345678910111'

fn fxx_to_str_l_parse #

@[direct_array_access; manualfree]
fn fxx_to_str_l_parse(s string) string

fxx_to_str_l_parse returns a string in decimal notation converted from a floating-point string in scientific notation.

Example

assert strconv.fxx_to_str_l_parse('34.22e+00') == '34.22'

fn fxx_to_str_l_parse_with_dot #

@[direct_array_access; manualfree]
fn fxx_to_str_l_parse_with_dot(s string) string

fxx_to_str_l_parse_with_dot returns a string in decimal notation converted from a floating-point string in scientific notation. If the decimal digits after the dot are zero, a '.0' is appended for clarity.

Example

assert strconv.fxx_to_str_l_parse_with_dot ('34.e+01') == '340.0'

fn parse_int #

fn parse_int(_s string, base int, _bit_size int) !i64

parse_int interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.

If the base argument is 0, the true base is implied by the string's prefix: 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise. Also, for argument base 0 only, underscore characters are permitted as defined by the Go syntax for integer literals.

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. If bitSize is below 0 or above 64, an error is returned.

fn parse_uint #

fn parse_uint(s string, _base int, _bit_size int) !u64

parse_uint is like parse_int but for unsigned numbers.

fn remove_tail_zeros #

@[direct_array_access]
fn remove_tail_zeros(s string) string

remove_tail_zeros strips trailing zeros from s and return the resulting string.

fn v_printf #

@[unsafe]
fn v_printf(str string, pt ...voidptr)

v_printf prints a sprintf-like formatted string to the terminal. The format string str can be constructed at runtime. Note, that this function is unsafe. In most cases, you are better off using V's string interpolation, when your format string is known at compile time.

fn v_sprintf #

@[direct_array_access; manualfree; unsafe]
fn v_sprintf(str string, pt ...voidptr) string

v_sprintf returns a sprintf-like formatted string. The format string str can be constructed at runtime. Note, that this function is unsafe. In most cases, you are better off using V's string interpolation, when your format string is known at compile time. Small integer and f32 arguments follow C-style default promotions.

Example

x := 3.141516
assert unsafe{strconv.v_sprintf('aaa %G', x)} == 'aaa 3.141516'

fn write_dec #

@[direct_array_access]
fn write_dec(n i64, mut buf []u8) int

write_dec writes the decimal representation of n into buf. It returns the number of bytes written, or -1 if buf is too small.

fn write_dec_u #

@[direct_array_access]
fn write_dec_u(n u64, mut buf []u8) int

write_dec_u writes the decimal representation of n into buf. It returns the number of bytes written, or -1 if buf is too small.

fn (Dec32) get_string_32 #

@[direct_array_access]
fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string

max 46 char -3.40282346638528859811704183484516925440e+38

type SeparatorOptions #

type SeparatorOptions = string | Separator

enum Align_text #

enum Align_text {
	right = 0
	left
	center
}

Align_text is used to describe the different ways to align a text - left, right and center

struct AtoF64Param #

@[params]
struct AtoF64Param {
pub:
	allow_extra_chars bool // allow extra characters after number
}

struct BF_param #

struct BF_param {
pub mut:
	pad_ch       u8   = u8(` `) // padding char
	len0         int  = -1      // default len for whole the number or string
	len1         int  = 6       // number of decimal digits, if needed
	positive     bool = true    // mandatory: the sign of the number passed
	sign_flag    bool // flag for print sign as prefix in padding
	align        Align_text = .right // alignment of the string
	rm_tail_zero bool // remove the tail zeros from floats
}

BF_param is used for describing the formatting options for a single interpolated value

struct Float32u #

union Float32u {
pub mut:
	f f32
	u u32
}

struct Float64u #

union Float64u {
pub mut:
	f f64
	u u64
}

struct PrepNumber #

struct PrepNumber {
pub mut:
	negative bool // 0 if positive number, 1 if negative
	exponent int  // power of 10 exponent
	mantissa u64  // integer mantissa
}

The structure is filled by parser, then given to converter.

struct Separator #

struct Separator {
pub:
	// integer is inserted every three digits in the integer part.
	integer string = ' '
	// decimal separates the integer and fractional parts.
	decimal string = '.'
}

Separator configures the separators used by numeric formatting functions. integer is inserted every three digits in the integer part, while decimal replaces the decimal point before the fractional part. An empty decimal keeps the default . decimal point.