You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -46,24 +46,155 @@ var out = format( str, 'world', 'Bob' );
46
46
// returns 'Hello, world! My name is Bob.'
47
47
```
48
48
49
-
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:
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:
50
+
51
+
%[flags][width][.precision]specifier
52
+
53
+
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.
54
+
55
+
```javascript
56
+
var str ='%s %s';
57
+
var out =format( str, 'Hello', 'World' );
58
+
// returns 'Hello World'
59
+
```
60
+
61
+
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:
62
+
63
+
%[pos$][flags][width][.precision]specifier
64
+
65
+
```javascript
66
+
var str ='%3$s %2$s %1$s';
67
+
var out =format( str, 'foo', 'bar', 'baz' );
68
+
// returns 'baz bar foo'
69
+
```
70
+
71
+
The following table summarizes the supported specifiers:
| - | left-justify the output within the given field width by padding with spaces on the right |
128
+
| 0 | left-pad the output with zeros instead of spaces when padding is required |
129
+
| # | use an alternative format for `o` and `x` conversions |
130
+
| + | prefix the output with a plus (+) or minus (-) sign even if the value is a positive number |
131
+
| space | prefix the value with a space character if no sign is written |
132
+
133
+
```javascript
134
+
var str ='Always prefix with a sign: %+i';
135
+
var out =format( str, 9 );
136
+
// returns 'Always prefix with a sign: +9'
137
+
138
+
out =format( str, -9 );
139
+
// returns 'Always prefix with a sign: -9'
140
+
141
+
str ='Only prefix with a sign if negative: %i';
142
+
out =format( str, 6 );
143
+
// returns 'Only prefix with a sign if negative: 6'
144
+
145
+
out =format( str, -6 );
146
+
// returns 'Only prefix with a sign if negative: -6'
147
+
148
+
str ='Prefix with a sign if negative and a space if positive: % i';
149
+
out =format( str, 3 );
150
+
// returns 'Prefix with a sign if negative and a space if positive: 3'
151
+
152
+
out =format( str, -3 );
153
+
// returns 'Prefix with a sign if negative and a space if positive: -3'
154
+
```
155
+
156
+
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.
157
+
158
+
```javascript
159
+
var str ='%5s';
160
+
var out =format( str, 'baz' );
161
+
// returns ' baz'
162
+
163
+
str ='%-5s';
164
+
out =format( str, 'baz' );
165
+
// returns 'baz '
166
+
167
+
str ='%05i';
168
+
out =format( str, 2 );
169
+
// returns '00002'
170
+
171
+
str ='%*i';
172
+
out =format( str, 5, 2 );
173
+
// returns ' 2'
174
+
```
175
+
176
+
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:
177
+
178
+
- For `s` specifiers, the `precision` specifies the maximum number of characters to be written to the output.
179
+
- 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).
180
+
- For `g` and `G` specifiers, the `precision` specifies the maximum number of significant digits to be written to the output.
181
+
- 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
182
+
183
+
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.
0 commit comments