Last active
August 14, 2024 14:19
-
-
Save DennyLoko/61882bc72176ca74a0f2 to your computer and use it in GitHub Desktop.
numberFormat Handlebars helper
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
/** | |
* 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
achei util.