-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.html
64 lines (49 loc) · 1.81 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text-html; charset=UTF-8" />
<script type="text/javascript" charset="utf-8" src="agh.sprintf.js"></script>
<script type="text/javascript" id="example-script">
// integers
printf("%d, %d", 12345, -12345); // "12345, -12345"
printf("%o, %o", 12345, -12345); // "30071, 37777747707"
printf("%u, %u", 12345, -12345); // "12345, 4294954951"
printf("%x, %X", 54321, 54321); // "d431, D431"
// floating-point numbers
var pi = Math.PI, infty = Number.POSITIVE_INFINITY;
printf("%f, %e, %g, %a", pi, pi, pi, pi); // "3.141593, 3.141593e+000, 3.14159, 0x1.921fb54442d18p+001"
printf("%f, %F", infty, infty); // "inf, INF"
// characters/strings
printf("%c, %s", 12354, 12354); // "あ, 12354"
// misc conversions
var arr = [];
printf("%p, %%, %n", 12345, arr); // "0x3039, %, "
console.log(arr[0]); // 11
// width
printf("[%8d][%*d][%*5$d]", 12345, 6, 12345, 12345, 7); // "[ 12345][ 12345][ 12345]"
// precision
printf("%.8d, %.*d, %.*5$d", 12345, 6, 12345, 12345, 7); // "00012345, 012345, 0012345"
printf("%.1g, %.10g", pi, pi); // "3, 3.141592654"
// flags
printf("%3d:%-3d:%03d", 1, 1, 1); // " 1:1 :001"
printf("%d:%+d:% d", 1, 1, 1); // "1:+1: 1"
printf("%#o:%#x:%#o:%#x", 1, 1, 0, 0); // "01:0x1:0:0"
printf("%d, %'d", 1234567, 1234567); // "1234567, 1,234,567"
// positional parameters
printf("%3$d %2$d %1$d %2$d %3$d", 111, 222, 333); // "333 222 111 222 333"
</script>
</head>
<body>
<h1>Demo</h1>
<pre id="example"></pre>
<style>#example {border: 1px solid gray; padding: 1em;}</style>
<script type="text/javascript">
(function() {
var pre = document.getElementById("example");
var scr = document.getElementById("example-script");
pre.textContent = scr.text;
})();
</script>
<p>Please see the console for the results.</p>
</body>
</html>