Skip to content

Instantly share code, notes, and snippets.

@resident-uhlig
Created December 11, 2013 08:37
Show Gist options
  • Save resident-uhlig/7906919 to your computer and use it in GitHub Desktop.
Save resident-uhlig/7906919 to your computer and use it in GitHub Desktop.
Patch for https://github.com/adamwdraper/Numeral-js/ to allow minimal length (before decimals)
Index: numeral.js
===================================================================
--- numeral.js (Revision 3089)
+++ numeral.js (Arbeitskopie)
@@ -235,12 +235,19 @@
precision,
thousands,
d = '',
- neg = false;
+ neg = false,
+ minlen = 0;
// check if number is zero and a custom zero format has been set
if (value === 0 && zeroFormat !== null) {
return zeroFormat;
} else {
+ // check for min length
+ if (format.indexOf('#') === 0) {
+ minlen = parseInt(format.substr(1, format.indexOf('#', 1) - 1)) || 0;
+ format = format.replace(/^#\d+#/, '');
+ }
+
// see if we should use parentheses for negative number or if we should prefix with a sign
// if both are present we default to parentheses
if (format.indexOf('(') > -1) {
@@ -356,6 +363,10 @@
neg = true;
}
+ if (w.length < minlen) {
+ w = Array(minlen - w.length + 1).join('0') + w;
+ }
+
if (thousands > -1) {
w = w.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + languages[currentLanguage].delimiters.thousands);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment