printf for typescript
npm install printf-ts
pure typescript implementation of printf-family with type checking
``typescript
import { printf } from 'printf-ts';
// Hello printf 1 1.20
printf("Hello %s %d %.2lf", "printf", 1, 1.2)
`
- 1 ~ 2 Returns the result as a string
- 3 ~ 4 Writes the results to the stdout, returns the number of characters printed
- 5 ~ 6 Writes the results to the stdout, returns the number of characters printed. if nodejs, append \n to the end(not affect to return value)
see cppreference-printf for each description of specifier
- flags: +, -, , #, 0width
- , precision*
- support wildcard()h
- length modifier: , hh, l, ll, L, j, z, t%c
- conversion specifier: , %s, %d, %i, %u, %o, %x, %X, %f, %F, %e, %E, %g, %G, %p, %%
- if argument is negative and format specifier is unsigned, it will not convert to unsigned type
- argument is not converted to a number of specific bits
- if origin type was unsigned, find it in signed type
| specifier | argument type | description |
| :---: | :---: | :--- |
| %c | string | str[0] ?? '' |%c
| | number | utf8 code point |%s
| | string | |%n
| | null | please use sprintf(...).length |
#### integer
> %d, %i, %u, %o, %x, %X
- short|int -> numberlong|long long|intmax_t|ssize_t|ptrdiff_t
- -> bigint
#### Floating point
> %f, %F, %e, %E, %g, %G
- double|long double -> numberinfinity
- if value in [, -infinity, NaN]F
- if specifier is uppercase(,E,G,A) => "INF", "-INF", "NAN""inf"
- else => , "-inf", "nan"
- NaN always treated as a positive value
#### Pointer
> %p
- void* -> bigint0n
- if value is => "(nil)""0x" + value.toString(16)
- else =>
-> number`