Skip to content

strconv #

Description

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

Constants #

const single_plus_zero = u32(00x00000000)

f32 constants

const single_minus_zero = u32(00x80000000)
const single_plus_infinity = u32(00x7F800000)
const single_minus_infinity = u32(00xFF800000)
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 atof64 #

fn atof64(s string) !f64

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

fn atof_quick #

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.

fn byte_to_lower #

fn byte_to_lower(c u8) u8

fn common_parse_int #

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 #

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 #

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 6 digits after the dot.

Example

assert strconv.f32_to_str_l(34.1234567) == '34.12346'

fn f32_to_str_l_with_dot #

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 6 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.) == '34.0'

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 #

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 #

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.) == '34.0'

fn f64_to_str_lnd1 #

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 #

fn format_dec_old(d u64, p BF_param) string

max int64 9223372036854775807

fn format_dec_sb #

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 #

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 #

fn format_fl(f f64, p BF_param) string

format_fl is a strings.Builder version of format_fl.

fn format_fl_old #

fn format_fl_old(f f64, p BF_param) string

strings.Builder version of format_fl

fn format_int #

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 #

fn format_str(s string, p BF_param) string

format_str returns a string 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_uint #

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 #

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 #

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 #

fn ftoa_long_32(f f32) string

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

Example

assert strconv.ftoa_long_32(34.1234567) == '34.12346'

fn ftoa_long_64 #

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 #

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 #

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 #

fn remove_tail_zeros(s string) string

remove_tail_zeros strips traling 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 formated 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 #

unsafe
fn v_sprintf(str string, pt ...voidptr) string

v_sprintf returns a sprintf-like formated 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.

Example

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

fn (Dec32) get_string_32 #

fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string

max 46 char -3.40282346638528859811704183484516925440e+38

enum Align_text #

enum Align_text {
	right  = 0
	left
	center
}

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
	allign       Align_text = .right @[deprecated: 'use align instead'; deprecated_after: '2023-11-30']
	rm_tail_zero bool // remove the tail zeros from floats
}

max float 1.797693134862315708145274237317043567981e+308

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.