-
-
Save vunb/00860a868c4d03c6909bdf0043ffecc1 to your computer and use it in GitHub Desktop.
numberFormat Handlebars helper
This file contains hidden or 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
/** | |
* An Handlebars helper to format numbers | |
* | |
* This helper have these three optional parameters: | |
* @var decimalLength int The length of the decimals | |
* @var thousandsSep char The thousands separator | |
* @var decimalSep char The decimals separator | |
* | |
* Based on: | |
* - mu is too short: http://stackoverflow.com/a/14493552/369867 | |
* - VisioN: http://stackoverflow.com/a/14428340/369867 | |
* | |
* Demo: http://jsfiddle.net/DennyLoko/6sR87/ | |
*/ | |
Handlebars.registerHelper('numberFormat', function (value, options) { | |
// Helper parameters | |
var dl = options.hash['decimalLength'] || 2; | |
var ts = options.hash['thousandsSep'] || ','; | |
var ds = options.hash['decimalSep'] || '.'; | |
// Parse to float | |
var value = parseFloat(value); | |
// The regex | |
var re = '\\d(?=(\\d{3})+' + (dl > 0 ? '\\D' : '$') + ')'; | |
// Formats the number with the decimals | |
var num = value.toFixed(Math.max(0, ~~dl)); | |
// Returns the formatted number | |
return (ds ? num.replace('.', ds) : num).replace(new RegExp(re, 'g'), '$&' + ts); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment