tot - javascript/sprintf.html
Not logged in
[Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Timeline
  [Raw
javascript/sprintf.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>POSIX sprintf(3) for JavaScript</title>
        <script type="text/javascript" src="sprintf.js"></script>
    </head>
    <body>
        <h1>POSIX sprintf(3) for JavaScript</h1>
        <script type="text/javascript">
            var tests = [
                /*  tests for %d  */
                [ "%d", 42, "42" ],
                [ "%2d", 42, "42" ],
                [ "%02d", 42, "42" ],
                [ "%3d", 42, " 42" ],
                [ "%03d", 42, "042" ],
                [ "%-3d", 42, "42 " ],
                [ "%-03d", 42, "42 " ],
                [ "%d", "xx", "0" ],
                /*  tests for %x  */
                [ "%x", 10, "a" ],
                [ "%2x", 10, " a" ],
                [ "%02x", 10, "0a" ],
                [ "%#02x", 10, "0xa" ],
                [ "%#04x", 10, "0x0a" ],
                /*  tests for %f  */
                [ "%f", 4.2, "4.2" ],
                [ "%.1f", 4.2, "4.2" ],
                [ "%.2f", 4.2, "4.20" ],
                [ "%8.4f", 4.12, "  4.1200" ],
                [ "%8.4f", 4.123456789, "  4.1235" ],
                /*  tests for %s  */
                [ "%2s", "test", "test" ],
                [ "%6s", "test", "  test" ],
                [ "%-6s", "test", "test  " ],
            ];
            for (var i = 0; i < tests.length; i++) {
                var output = sprintf(tests[i][0], tests[i][1]);
                var msg = "sprintf(\""+tests[i][0]+"\", "+tests[i][1]+") = \""+output+"\"";
                if (output != tests[i][2])
                    msg = msg + " ERROR: expected \""+tests[i][2]+"\"";
                msg = msg + "<br/>\n";
                document.write(msg);
            }
        </script>
    </body>
</html>