About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Usage
var format = require( '@stdlib/string-format' );
format( str, ...args )
Inserts supplied variable values into a format string.
var str = 'Hello, %s! My name is %s.';
var out = format( str, 'world', 'Bob' );
The format string is a string literal containing zero or more conversion specifications, each of which results in a string value being inserted to the output string. A conversion specification consists of a percent sign (%
) followed by one or more of the following flags, width, precision, and conversion type characters. It thus takes the following form:
%[flags][width][.precision]specifier
Arguments following the format string are used to replace the placeholders in the format string. The number of arguments following the format string should be equal to the number of placeholders in the format string.
var str = '%s %s';
var out = format( str, 'Hello', 'World' );
To supply arguments in a different order than they appear in the format string, positional placeholders as indicated by a $
character in the format string are used. In this case, the conversion specification takes the form:
%[pos$][flags][width][.precision]specifier
var str = '%3$s %2$s %1$s';
var out = format( str, 'foo', 'bar', 'baz' );
The following table summarizes the supported specifiers:
type | description | example |
---|
s | string | beep boop |
c | character | a |
d, i | signed decimal integer | -12 |
u | unsigned decimal integer | 390 |
b | unsigned binary integer | 11011011 |
o | unsigned octal integer | 510 |
x | unsigned hexadecimal (lowercase) | 7b |
X | unsigned hexadecimal (uppercase) | 7B |
f, F | decimal floating point | 390.24 |
e | scientific notation (lowercase) | 3.9e+1 |
E | scientific notation (uppercase) | 3.9E+1 |
g | shortest representation (e /f ) | 3.9 |
G | shortest representation (E /F ) | 3.9 |
var str = '%i written as a binary number is %b.';
var out = format( str, 9, 9 );
str = '%i written as an octal number is %o.';
out = format( str, 17, 17 );
str = '%i written as a hexadecimal number is %x.';
out = format( str, 255, 255 );
str = '%i written as a hexadecimal number is %X (uppercase letters).';
out = format( str, 255, 255 );
str = '%i written as a floating point number with default precision is %f!';
out = format( str, 8, 8 );
str = 'Scientific notation: %e';
out = format( str, 3.14159 );
str = 'Scientific notation: %E (uppercase).';
out = format( str, 3.14159 );
str = '%g (shortest representation)';
out = format( str, 3.14159 );
A conversion specification may contain zero or more flags, which modify the behavior of the conversion. The following flags are supported:
flag | description |
---|
- | left-justify the output within the given field width by padding with spaces on the right |
0 | left-pad the output with zeros instead of spaces when padding is required |
# | use an alternative format for o and x conversions |
+ | prefix the output with a plus (+) or minus (-) sign even if the value is a positive number |
space | prefix the value with a space character if no sign is written |
var str = 'Always prefix with a sign: %+i';
var out = format( str, 9 );
out = format( str, -9 );
str = 'Only prefix with a sign if negative: %i';
out = format( str, 6 );
out = format( str, -6 );
str = 'Prefix with a sign if negative and a space if positive: % i';
out = format( str, 3 );
out = format( str, -3 );
The width
may be specified as a decimal integer representing the minimum number of characters to be written to the output. If the value to be written is shorter than this number, the result is padded with spaces on the left. The value is not truncated even if the result is larger. Alternatively, the width
may be specified as an asterisk character (*
), in which case the argument preceding the conversion specification is used as the minimum field width.
var str = '%5s';
var out = format( str, 'baz' );
str = '%-5s';
out = format( str, 'baz' );
str = '%05i';
out = format( str, 2 );
str = '%*i';
out = format( str, 5, 2 );
The precision
may be specified as a decimal integer or as an asterisk character (*
), in which case the argument preceding the conversion specification is used as the precision value. The behavior of the precision
differs depending on the conversion type:
- For
s
specifiers, the precision
specifies the maximum number of characters to be written to the output. - For floating point specifiers (
f
, F
, e
, E
), the precision
specifies the number of digits after the decimal point to be written to the output (by default, this is 6
). - For
g
and G
specifiers, the precision
specifies the maximum number of significant digits to be written to the output. - For integer specifiers (
d
, i
, u
, b
, o
, x
, X
), the precision
specifies the minimum number of digits to be written to the output. If the value to be written is shorter than this number, the result is padded with zeros on the left. The value is not truncated even if the result is longer. For
Alternatively, the precision
may be specified as an asterisk character (*
), in which case the argument preceding the conversion specification is used as the minimum number of digits.
var str = '%5.2s';
var out = format( str, 'baz' );
str = 'PI: ~%.2f';
out = format( str, 3.14159 );
str = 'Agent %.3i';
out = format( str, 7 );