Namespace Pot.Format
Defined in: <pot.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Format utilities.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
Pot.Format.format(fmt)
Formats to a string by arguments.
|
<static> |
Pot.Format.sprintf(format)
sprintf.
|
Method Detail
<static>
{Function}
Pot.Format.format(fmt)
Formats to a string by arguments.
var result = format('#1 + #2 + #3', 10, 20, 30); debug(result); // @results '10 + 20 + 30'
var result = format('J#1v#1#2 ECMA#2', 'a', 'Script'); debug(result); // @results 'JavaScript ECMAScript'
- Parameters:
- {String} fmt
- A string format.
- {...*} (...)
- Format arguments.
- Returns:
- {String} Returns a formatted string.
<static>
{Function}
Pot.Format.sprintf(format)
sprintf.
This function is compatible with PHP sprintf function that
was referenced from the PHP source code.
var num = 5; var place = 'tree'; var result = sprintf('There are %d monkeys in the %s.', num, place); debug(result); // @results 'There are 5 monkeys in the tree.'
var n = 43951789; var u = -43951789; var c = 65; // ASCII 65 is 'A' // notice the double %%, this prints a literal '%' character debug(sprintf("%%b = '%b'", n)); // binary debug(sprintf("%%c = '%c'", c)); // print the ascii character debug(sprintf("%%d = '%d'", n)); // standard integer debug(sprintf("%%e = '%e'", n)); // scientific notation debug(sprintf("%%u = '%u'", n)); // unsigned integer (positive) debug(sprintf("%%u = '%u'", u)); // unsigned integer (negative) debug(sprintf("%%f = '%f'", n)); // floating point debug(sprintf("%%o = '%o'", n)); // octal debug(sprintf("%%s = '%s'", n)); // string debug(sprintf("%%x = '%x'", n)); // hexadecimal (lower-case) debug(sprintf("%%X = '%X'", n)); // hexadecimal (upper-case) debug(sprintf("%%+d = '%+d'", n)); // sign specifier (positive) debug(sprintf("%%+d = '%+d'", u)); // sign specifier (negative) debug(sprintf("%%a = '%a'", n)); // base 36 format (lower-case) debug(sprintf("%%A = '%A'", n)); // base 36 format (upper-case) // @results // %b = '10100111101010011010101101' // %c = 'A' // %d = '43951789' // %e = '4.395179e+7' // %u = '43951789' // %u = '4251015507' // %f = '43951789.000000' // %o = '247523255' // %s = '43951789' // %x = '29ea6ad' // %X = '29EA6AD' // %+d = '+43951789' // %+d = '-43951789' // %a = 'q61f1' // %A = 'Q61F1'
var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var isoDate = sprintf('%04d-%02d-%02d', year, month, day); debug(isoDate); // @results '2011-09-01'
var s = 'monkey'; var t = 'many monkeys'; debug(sprintf("[%s]", s)); // standard string output debug(sprintf("[%10s]", s)); // right-justification with spaces debug(sprintf("[%-10s]", s)); // left-justification with spaces debug(sprintf("[%010s]", s)); // zero-padding works on strings too debug(sprintf("[%'#10s]", s)); // use the custom padding character '#' debug(sprintf("[%10.10s]", t)); // left-justification but with a // cutoff of 10 characters // @results // [monkey] // [ monkey] // [monkey ] // [0000monkey] // [####monkey] // [many monke]
Pot.debug(Pot.sprintf('%n', 1234567890)); // 1,234,567,890 Pot.debug(Pot.sprintf('%n', 12345678.9)); // 12,345,678.9 Pot.debug(Pot.sprintf('%n', 123)); // 123
- Parameters:
- {String} format
- The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter.
- {...*} (...)
- The variable arguments that will be conversion specifier value.
- Returns:
- {String} The result string.
- See:
- http://php.net/function.sprintf
Extended type specifiers: - a : Return the string in lowercase the result encoded in base 36. - A : Return the string in uppercase the result encoded in base 36. - n : Return the string that is formatted number with grouped thousands.