Skip to content

Commit 0b8bf19

Browse files
deal with formatting
1 parent 6084428 commit 0b8bf19

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

source/js/PivotView.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var PivotView = function (controller, container) {
1010

1111
this.tablesStack = [];
1212

13+
numeral.call(this);
14+
1315
this.elements = {
1416
container: container,
1517
base: document.createElement("div"),
@@ -752,15 +754,21 @@ PivotView.prototype.renderRawData = function (data) {
752754

753755
this.SEARCH_ENABLED = SEARCH_ENABLED;
754756

757+
this.numeral.setup(
758+
info["decimalSeparator"] || ".",
759+
info["numericGroupSeparator"] || ",",
760+
info["numericGroupSize"] || 3
761+
);
762+
755763
var formatContent = function (value, element, format) {
756764
if (!isFinite(value)) {
757765
element.className += " formatLeft";
758766
element.textContent = value || "";
759767
} else { // number
760768
if (format) { // set format
761-
element.textContent = value ? numeral(value).format(format) : "";
769+
element.textContent = value ? _.numeral(value).format(format) : "";
762770
} else if (value && info.defaultFormat) {
763-
element.textContent = numeral(value).format(
771+
element.textContent = _.numeral(value).format(
764772
info.defaultFormat[value % 1 === 0 ? "int" : "double"]
765773
);
766774
} else {

source/js/numeral.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* http://adamwdraper.github.com/Numeral-js/
77
*/
88

9-
(function () {
9+
var numeral = function () {
1010

1111
/************************************
1212
Constants
@@ -20,6 +20,10 @@
2020
zeroFormat = null,
2121
defaultFormat = '0,0';
2222

23+
var NUMBER_GROUP_LENGTH = 3,
24+
NUMBER_GROUP_SEPARATOR = ",",
25+
DECIMAL_SEPARATOR = ".",
26+
NUM_REGEX = new RegExp("(\\d)(?=(\\d{" + NUMBER_GROUP_LENGTH + "})+(?!\\d))", "g");
2327

2428
/************************************
2529
Constructors
@@ -356,7 +360,7 @@
356360
w = d.split('.')[0];
357361

358362
if (d.split('.')[1].length) {
359-
d = languages[currentLanguage].delimiters.decimal + d.split('.')[1];
363+
d = (DECIMAL_SEPARATOR || languages[currentLanguage].delimiters.decimal) + d.split('.')[1];
360364
} else {
361365
d = '';
362366
}
@@ -375,7 +379,8 @@
375379
}
376380

377381
if (thousands > -1) {
378-
w = w.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + languages[currentLanguage].delimiters.thousands);
382+
w = w.toString().replace(NUM_REGEX, '$1'
383+
+ (NUMBER_GROUP_SEPARATOR || languages[currentLanguage].delimiters.thousands));
379384
}
380385

381386
if (format.indexOf('.') === 0) {
@@ -574,6 +579,15 @@
574579
Numeral Prototype
575580
************************************/
576581

582+
numeral.setup = function (decimalSeparator, numberGroupSeparator, numberGroupLength) {
583+
if (decimalSeparator !== DECIMAL_SEPARATOR) DECIMAL_SEPARATOR = decimalSeparator;
584+
if (numberGroupSeparator !== NUMBER_GROUP_SEPARATOR)
585+
NUMBER_GROUP_SEPARATOR = numberGroupSeparator;
586+
if (numberGroupLength !== NUMBER_GROUP_LENGTH) {
587+
NUMBER_GROUP_LENGTH = numberGroupLength;
588+
NUM_REGEX = new RegExp("(\\d)(?=(\\d{" + NUMBER_GROUP_LENGTH + "})+(?!\\d))", "g");
589+
}
590+
};
577591

578592
numeral.fn = Numeral.prototype = {
579593

@@ -653,4 +667,4 @@
653667

654668
this.numeral = numeral;
655669

656-
}).call(this);
670+
};

0 commit comments

Comments
 (0)