-
-
Save javouhey/8468946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A sprintf-like function using d3.js | |
// https://gist.github.com/2045912 | |
function d3_sprintf(strings, formatters) { | |
return formatters.reduce(function (strings, formatter, i) { | |
strings[i] = formatter(strings[i]); | |
return strings | |
}, strings).join(""); | |
}; | |
d3_sprintf( | |
["This is PI to two digits: ", Math.PI, ", while this is PI to six digits: ", Math.PI], | |
[String, d3.format("2.2f"), String, d3.format("2.6f")] | |
); | |
// => "This is PI to two digits: 3.14, while this is PI to six digits: 3.141593" | |
d3_sprintf( | |
["Today is: ", new Date(), | |
", but if you want to get all technical it's also: ", new Date(), | |
", which is the ", new Date(), "th week of the year."], | |
[String, d3.time.format("%Y-%m-%d"), String, d3.time.format.iso, String, d3.time.format("%W")] | |
); | |
// => "Today is: 2012-03-15, but if you want to get all technical it's also: 2012-03-15T19:29:39.673Z, which is the 11th week of the year." | |
d3_sprintf( | |
["Milliseconds since Jan 1, 1970 in exponential notation: ", Date.now(), | |
", in SI units: ", Date.now()], | |
[String, d3.format("1.3e"), String, d3.format("1.3s")] | |
); | |
// => "Milliseconds since Jan 1, 1970 in exponential notation: 1.332e+12, in SI units: 1.33T" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment