https://github.com/knowledgecode/format.js
a format function like String#format() in Java
https://github.com/knowledgecode/format.js
Last synced: 3 months ago
JSON representation
a format function like String#format() in Java
- Host: GitHub
- URL: https://github.com/knowledgecode/format.js
- Owner: knowledgecode
- License: mit
- Created: 2013-01-02T09:01:16.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-01-04T13:05:25.000Z (over 12 years ago)
- Last Synced: 2025-01-30T19:52:28.993Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 129 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Format.js
Format.js is a simple JavaScript format function, like a Java String.format.
It works in Chrome, Firefox, Safari, Opera, and IE 6.0+.## Usage
### String
`%s`var s = format('%s world.', 'Hello');
console.log(s); // 'Hello world.'`%8s` (Left padding)
var s = format('%8s world.', 'Hello');
console.log(s); // ' Hello world.'`%-8s` (Right padding)
var s = format('%-8s world.', 'Hello');
console.log(s); // 'Hello world.'### Number
`%d`var s = format('%d billion people.', 7.062);
console.log(s); // '7.062 billion people.'`%5d` (Left padding)
var s = format('%5d billion people.', 7);
console.log(s); // ' 7 billion people.'`%-5d` (Right padding)
var s = format('%-5d billion people.', 7);
console.log(s); // '7 billion people.'`%03d` (Zero padding)
var s = format('%03d billion people.', 7);
console.log(s); // '007 billion people.'`%o` (Octal notation)
var s = format('0%o', 100);
console.log(s); // '0144'`%x` (Hex notation)
var s = format('0x%x', 255);
console.log(s); // '0xff'### Escape
`%%d`var s = format('%%d to %d', 100);
console.log(s); // '%d to 100'## License
Format.js is available under the terms of the MIT license.