2020-04-29 12:25:38 +03:00
# v_printf/v_sprintf
These are v implementations of the C language `printf` and `sprintf` functions.
2023-02-13 11:29:02 +03:00
> **Note**
> These functions are platform dependent in C, but in V they are platform independent.
2020-04-29 12:25:38 +03:00
### v_sprintf
2020-11-18 20:28:28 +03:00
`v_sprintf` has a variable number of parameters.
The first is a format string to control the appearance of the final string.
Each format specifier (%s, %d, etc.) in the format string
is replaced by the textual version of the following parameters.
2020-04-29 12:25:38 +03:00
```v
import strconv
2020-12-06 00:54:41 +03:00
2020-04-29 12:25:38 +03:00
fn main() {
2020-12-06 00:54:41 +03:00
a := 'World'
s := strconv.v_sprintf('Hello %s!', a)
2020-04-29 12:25:38 +03:00
println(s)
}
```
```
Hello World!
```
### v_printf
2020-11-18 20:28:28 +03:00
`v_printf` creates the same modified string as `v_sprintf` , using the same format specifiers,
but it will immediately print the modified string to stdout instead of returning a string.
2020-04-29 12:25:38 +03:00
### Syntax
The syntax for a format specifier is:
```
%[parameter][flags][width][.precision][length]type
```
#### Flags field
The Flags field may be zero or more (in any order) of:
| Character | Description |
| ----------- | ------------------------------------------------------------ |
| `-` (minus) | Left-align the output of this specifier. (The default is to right-align the output.) |
| `+` (plus) | Prepends a plus for positive signed-numeric types. positive = `+` , negative = `-` . (The default doesn't prepend anything to positive numbers.) |
| `0` (zero) | When the 'width' option is specified, prepends zeros for numeric types. (The default prepends spaces.) For example, `printf("%4X",3)` produces ` 3` , while `printf("%04X",3)` produces `0003` . |
#### Width field
2020-12-06 00:54:41 +03:00
The Width field specifies a *maximum* number of characters to output,
and is typically used to pad fixed-width fields in tabulated output,
2020-11-18 20:28:28 +03:00
it causes truncation of oversized fields.
2020-04-29 12:25:38 +03:00
2020-12-06 00:54:41 +03:00
The width field may be omitted, or it may be a numeric integer value,
or may also be specified by a parameter when indicated by an asterisk `*` .
For example, `v_printf("%*.s", 5, my_string)` will result in ` mystring` being printed,
2020-11-18 20:28:28 +03:00
with a total width of 5 characters.
2020-04-29 12:25:38 +03:00
2020-12-06 00:54:41 +03:00
#### Length field
2020-04-29 12:25:38 +03:00
The Length field can be omitted or be any of:
| Character | Description |
| --------- | ------------------------------------------------------------ |
| `hh` | For integer types, causes `printf` to expect an `byte` or `i8` argument. |
| `h` | For integer types, causes `printf` to expect an `int16` or `u16` argument. |
| `l` | For integer types, causes `printf` to expect an `i64` or `u64` argument. |
| `ll` | For integer types, causes `printf` to expect an `i64` or `u64` argument. |
| | |
| | |
2020-12-06 00:54:41 +03:00
#### Type field
2020-04-29 12:25:38 +03:00
The Type field can be any of:
| Character | Description |
| --------- | ------------------------------------------------------------ |
| `%` | Prints a literal `%` character (this type doesn't accept any flags, width, precision, length fields). |
| `d` , `i` | `int` as a signed `int` `%d` and `%i` are synonymous for output. The size of the argument is specified by the length field. |
| `u` | `unsigned int` . The size of the argument is specified by the length field. |
| `f` , `F` | `double` in normal notation. `f` and `F` only differs in how the strings are printed: lowercase or uppercase. |
| `e` , `E` | `double` in scientific notation.`e` and `E` only differs in how the strings are printed: lowercase or uppercase. |
| `g` , `G` | `double` in automatic notation.`g` and `G` only differs in how the strings are printed: lowercase or uppercase. |
| `x` , `X` | `unsigned int` as a hexadecimal number. `x` uses lower-case letters and `X` uses upper-case. |
| `s` | string |
| `p` | `void *` (pointer to void) in an implementation-defined format. |
| `c` | `char` (character). |
## Examples
various types
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-12-06 00:54:41 +03:00
a0 := u32(10)
b0 := 200
c0 := byte(12)
s0 := 'ciAo'
2020-04-29 12:25:38 +03:00
ch0 := `B`
2020-12-06 00:54:41 +03:00
f0 := 0.312345
f1 := 200000.0
sc0 := 'ciao: [%-08u] %d %hhd [%8s] [%08X] [%-20.4f] [%-20.4f] [%c]'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc0, a0, b0, c0, s0, b0, f0, f1, ch0)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
ciao: [10 ] 200 12 [ ciAo] [000000C8] [0.3123 ] [200000.0000 ] [B]
```
integer
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-04-29 12:25:38 +03:00
a := byte(12)
b := i16(13)
c := 14
d := i64(15)
2020-12-06 00:54:41 +03:00
sc1 := '==>%hhd %hd %d %ld'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc1, a, b, c, d)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
==>12 13 14 15
```
unsigned integer
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-04-29 12:25:38 +03:00
a1 := byte(0xff)
b1 := u16(0xffff)
2020-12-06 00:54:41 +03:00
c1 := u32(0xffffffff)
2020-04-29 12:25:38 +03:00
d1 := u64(-1)
2020-12-06 00:54:41 +03:00
sc2 := '%hhu %hu %u %lu'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc2, a1, b1, c1, d1)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
255 65535 4294967295 18446744073709551615
```
hexadecimal
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-04-29 12:25:38 +03:00
a1 := byte(0xff)
b1 := i16(0xffff)
2020-12-06 00:54:41 +03:00
c1 := u32(0xffffffff)
2020-04-29 12:25:38 +03:00
d1 := u64(-1)
2020-12-06 00:54:41 +03:00
sc3 := '%hhx %hx %x %lx'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc3, a1, b1, c1, d1)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
ff ffff ffffffff ffffffffffffffff
```
hexadecimal
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-04-29 12:25:38 +03:00
a2 := 125
2020-12-06 00:54:41 +03:00
sc7 := '[%9x] [%9X] [%-9x] [%-9X] [%09x] [%09X]'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc7, a2, a2, a2, a2, a2, a2)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
[ 7d] [ 7D] [7d ] [7D ] [00000007d] [00000007D]
```
floating points
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-12-06 00:54:41 +03:00
f0 := 0.312345
f1 := 200000.0
f2 := -1234.300e6
f3 := 1234.300e-6
sc4 := '[%-20.3e] [%20.3e] [%-020.3e] [%-020.3E] [%-020.3e] [%-020.3e]'
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc4, f0, f1, f1, f1, f2, f3)
2020-04-29 12:25:38 +03:00
println(temp_s)
```
```
[3.123e-01 ] [ 2.000e+05] [2.000e+05 ] [2.000E+05 ] [-1.234e+09 ] [1.234e-03 ]
```
float automatic notations
2021-12-01 18:23:35 +03:00
```v
import strconv
2020-04-29 12:25:38 +03:00
mut ft := -1e-7
2020-12-06 00:54:41 +03:00
mut x := 0
sc8 := '[%20g][%20G]|'
2020-04-29 12:25:38 +03:00
for x < 12 {
2021-12-01 18:23:35 +03:00
temp_s := strconv.v_sprintf(sc8, ft, ft)
2022-11-15 16:53:13 +03:00
println('${temp_s}\n')
2020-04-29 12:25:38 +03:00
ft = ft * 10.0
x++
}
```
```
[ -1e-07][ -1E-07]|
[ -1e-06][ -1E-06]|
[ -1e-05][ -1E-05]|
[ -0.0001][ -0.0001]|
[ -0.001][ -0.001]|
[ -0.01][ -0.01]|
[ -0.1][ -0.1]|
[ -1][ -1]|
[ -10][ -10]|
[ -100][ -100]|
[ -1000][ -1000]|
[ -10000][ -10000]|
```
## Utility functions
The format module also has some utility functions:
2020-12-06 00:54:41 +03:00
```v oksyntax nofmt
2020-04-29 12:25:38 +03:00
// calling struct
struct BF_param {
pad_ch byte = ` ` // padding char
len0 int = -1 // default len for whole the number or string
len1 int = 6 // number of decimal digits, if needed
2020-12-06 00:54:41 +03:00
positive bool = true // mandatory: the sign of the number passed
2020-04-29 12:25:38 +03:00
sign_flag bool = false // flag for print sign as prefix in padding
allign Align_text = .right // alignment of the string
rm_tail_zero bool = false // remove the tail zeros from floats
}
// utilities
fn format_dec(d u64, p BF_param) string
fn format_fl(f f64, p BF_param) string
fn format_es(f f64, p BF_param) string
fn remove_tail_zeros(s string) string
```
`format_dec` format the integer number using the parameters in the `BF_param` struct.
`format_fl` format a float number in normal notation using the parameters in the `BF_param` struct.
2020-12-06 00:54:41 +03:00
`format_es format a float number in scientific notation using the parameters in the BF_param`
2020-11-18 20:28:28 +03:00
struct.
2020-04-29 12:25:38 +03:00
2020-12-06 00:54:41 +03:00
`remove_tail_zeros` removes the tailing zeros from a floating point number as string.