README.md (8885B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2022 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # format 22 23 > Insert supplied variable values into a format string. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var format = require( '@stdlib/string/format' ); 37 ``` 38 39 #### format( str, ...args ) 40 41 Inserts supplied variable values into a format string. 42 43 ```javascript 44 var str = 'Hello, %s! My name is %s.'; 45 var out = format( str, 'world', 'Bob' ); 46 // returns 'Hello, world! My name is Bob.' 47 ``` 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. It thus takes the following form: 50 51 ```text 52 %[flags][width][.precision]specifier 53 ``` 54 55 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. 56 57 ```javascript 58 var str = '%s %s'; 59 var out = format( str, 'Hello', 'World' ); 60 // returns 'Hello World' 61 ``` 62 63 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: 64 65 ```text 66 %[pos$][flags][width][.precision]specifier 67 ``` 68 69 ```javascript 70 var str = '%3$s %2$s %1$s'; 71 var out = format( str, 'foo', 'bar', 'baz' ); 72 // returns 'baz bar foo' 73 ``` 74 75 The following table summarizes the supported specifiers: 76 77 | type | description | example | 78 | ---- | ---------------------------------- | ------------ | 79 | s | string | beep boop | 80 | c | character | a | 81 | d, i | signed decimal integer | -12 | 82 | u | unsigned decimal integer | 390 | 83 | b | unsigned binary integer | 11011011 | 84 | o | unsigned octal integer | 510 | 85 | x | unsigned hexadecimal (lowercase) | 7b | 86 | X | unsigned hexadecimal (uppercase) | 7B | 87 | f, F | decimal floating point | 390.24 | 88 | e | scientific notation (lowercase) | 3.9e+1 | 89 | E | scientific notation (uppercase) | 3.9E+1 | 90 | g | shortest representation (`e`/`f`) | 3.9 | 91 | G | shortest representation (`E`/`F`) | 3.9 | 92 93 ```javascript 94 var str = '%i written as a binary number is %b.'; 95 var out = format( str, 9, 9 ); 96 // returns '9 written as a binary number is 1001.' 97 98 str = '%i written as an octal number is %o.'; 99 out = format( str, 17, 17 ); 100 // returns '17 written as an octal number is 21.' 101 102 str = '%i written as a hexadecimal number is %x.'; 103 out = format( str, 255, 255 ); 104 // returns '255 written as a hexadecimal number is ff.' 105 106 str = '%i written as a hexadecimal number is %X (uppercase letters).'; 107 out = format( str, 255, 255 ); 108 // returns '255 written as a hexadecimal number is FF (uppercase letters).' 109 110 str = '%i written as a floating point number with default precision is %f!'; 111 out = format( str, 8, 8 ); 112 // returns '8 written as a floating point number with default precision is 8.000000!' 113 114 str = 'Scientific notation: %e'; 115 out = format( str, 3.14159 ); 116 // returns 'Scientific notation: 3.141590e+00' 117 118 str = 'Scientific notation: %E (uppercase).'; 119 out = format( str, 3.14159 ); 120 // returns 'Scientific notation: 3.141590E+00 (uppercase).' 121 122 str = '%g (shortest representation)'; 123 out = format( str, 3.14159 ); 124 // returns '3.14159' 125 ``` 126 127 A conversion specification may contain zero or more flags, which modify the behavior of the conversion. The following flags are supported: 128 129 | flag | description | 130 | ----- | ------------------------------------------------------------------------------------------ | 131 | - | left-justify the output within the given field width by padding with spaces on the right | 132 | 0 | left-pad the output with zeros instead of spaces when padding is required | 133 | # | use an alternative format for `o` and `x` conversions | 134 | + | prefix the output with a plus (+) or minus (-) sign even if the value is a positive number | 135 | space | prefix the value with a space character if no sign is written | 136 137 ```javascript 138 var str = 'Always prefix with a sign: %+i'; 139 var out = format( str, 9 ); 140 // returns 'Always prefix with a sign: +9' 141 142 out = format( str, -9 ); 143 // returns 'Always prefix with a sign: -9' 144 145 str = 'Only prefix with a sign if negative: %i'; 146 out = format( str, 6 ); 147 // returns 'Only prefix with a sign if negative: 6' 148 149 out = format( str, -6 ); 150 // returns 'Only prefix with a sign if negative: -6' 151 152 str = 'Prefix with a sign if negative and a space if positive: % i'; 153 out = format( str, 3 ); 154 // returns 'Prefix with a sign if negative and a space if positive: 3' 155 156 out = format( str, -3 ); 157 // returns 'Prefix with a sign if negative and a space if positive: -3' 158 ``` 159 160 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. 161 162 ```javascript 163 var str = '%5s'; 164 var out = format( str, 'baz' ); 165 // returns ' baz' 166 167 str = '%-5s'; 168 out = format( str, 'baz' ); 169 // returns 'baz ' 170 171 str = '%05i'; 172 out = format( str, 2 ); 173 // returns '00002' 174 175 str = '%*i'; 176 out = format( str, 5, 2 ); 177 // returns ' 2' 178 ``` 179 180 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: 181 182 - For `s` specifiers, the `precision` specifies the maximum number of characters to be written to the output. 183 - 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`). 184 - For `g` and `G` specifiers, the `precision` specifies the maximum number of significant digits to be written to the output. 185 - 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 186 187 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. 188 189 ```javascript 190 var str = '%5.2s'; 191 var out = format( str, 'baz' ); 192 // returns ' ba' 193 194 str = 'PI: ~%.2f'; 195 out = format( str, 3.14159 ); 196 // returns 'PI: ~3.14' 197 198 str = 'Agent %.3i'; 199 out = format( str, 7 ); 200 // returns 'Agent 007' 201 ``` 202 203 </section> 204 205 <!-- /.usage --> 206 207 <section class="examples"> 208 209 ## Examples 210 211 <!-- eslint no-undef: "error" --> 212 213 ```javascript 214 var format = require( '@stdlib/string/format' ); 215 216 var out = format( '%s %s!', 'Hello', 'World' ); 217 // returns 'Hello World!' 218 219 out = format( 'Pi: ~%.2f', 3.141592653589793 ); 220 // returns 'Pi: ~3.14' 221 222 out = format( '%-10s %-10s', 'a', 'b' ); 223 // returns 'a b ' 224 225 out = format( '%10s %10s', 'a', 'b' ); 226 // returns ' a b' 227 228 out = format( '%2$s %1$s %3$s', 'b', 'a', 'c' ); 229 // returns 'a b c' 230 ``` 231 232 </section> 233 234 <!-- /.examples --> 235 236 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 237 238 <section class="related"> 239 240 </section> 241 242 <!-- /.related --> 243 244 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 245 246 <section class="links"> 247 248 </section> 249 250 <!-- /.links -->