Based on the functionality of the Perl implementation of sprintf.
The format is specified using the %{flags}{type} convention, for example %s to
insert a string at the position. Following types is possible (faithfully
stolen from perldoc):
-
%%a percent sign -
%ca character with the given number -
%sa string -
%da signed integer, in decimal -
%uan unsigned integer, in decimal -
%oan unsigned integer, in octal -
%xan unsigned integer, in hexadecimal -
%ea floating-point number, in scientific notation -
%fa floating-point number, in fixed decimal notation -
%ga floating-point number, in%eor%fnotation -
%Xlike%x, but using upper-case letters -
%Elike%e, but using an upper-caseE -
%Glike%g, but with an upper-caseE(if applicable) -
%ban unsigned integer, in binary -
%Blike%b, but using an upper-caseBwith the # flag
Following flags exists:
-
spaceprefix positive number with a space -
+prefix positive number with a plus sign -
-left-justify within the field -
0use zeros, not spaces, to right-justify -
#ensure the leading0for any octal, prefix non-zero hexadecimal with0xor0X, prefix non-zero binary with0bor0B -
@- if applying vectorization to an Number, then@Nwill define the bitwidth
see perldoc -f sprintf for more information about how to use the flags. This
module handles mosly everything the perl module handles, except the %n and %p
types and size modifiers for integers.
Following global functions is exported
Will print out the string to the console, or using window.dump()
Will print out the string to the console, or using window.dump()
Will return a formated string
Will return a formated string
Will append the formated string.
Will append the formated string.
Will format the objects in question with given arguments
Will format the objects in question with given arguments
$('obj').printf('<div>Integer: %d, hex: %1$#x, HEX: %1$#X, Binary: %1$#b,
BINARY: %1$#B, Octal: %1$#o, Float: %1$f, Exp: %1$e</div>', 123.456 );
results in
Integer: 123, hex: 0x7b, HEX: 0x7B, Binary: 0b1111011, BINARY: 0B1111011,
Octal: 0173, Float: 123.456, Exp: 1.234560e+2
appended to the objects in question.
$('obj').printf("%0*v8b", ' ', "12345" );
results in
00110001 00110010 00110011 00110100 00110101
appended to the objects in question.
$('#target').printf('<div>The color <strong>#%*2$vX</strong> can also be
written as an RGB triplet: <strong>rgb(%*3$v1$d)</strong></div>',
String.fromCharCode(0x33,0xab,0x67), '', ', ' );
$('#target').printf('<div>The color <strong>#%*2$vX</strong> can also be
written as an RGB triplet: <strong>rgb(%*3$v1$d)</strong></div>',
[0x33,0xab,0x67], '', ', ' );
$('#target').printf('<div>The color <strong>#%@8*2$vX</strong> can also be
written as an RGB triplet: <strong>rgb(%@8*3$v1$d)</strong></div>', 0x33ab67,
'', ', ' );
$('#target').printf('<div>The color <strong>#%@8*2$vX</strong> can also be
written as an RGB triplet: <strong>rgb(%@8*3$v1$d)</strong></div>', 3386215,
'', ', ' );
will all results in
The color <strong>#33AB67</strong> can also be written as an RGB triplet:
<strong>rgb(51, 171, 103)</strong>
(perhaps not the most useful code, but it shows some sort of possibility :))
$('.target').format( 'Hello', 'World' );
with the html
<div class="target">
<div>
<strong>%s</strong> <em>%s</em>
</div>
</div>
will result in
<div class="target">
<div>
<strong>Hello</strong> <em>World</em>
</div>
</div>