diff options
author | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2017-04-19 22:52:43 +0300 |
---|---|---|
committer | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2017-04-19 22:52:43 +0300 |
commit | 54c53020c3e0ffd9ddd05b40743c0781cff3dd8f (patch) | |
tree | d2029ceaa9c1b4980c233cc45e928c9e6368344f /web | |
parent | 2a818270ae2a1c80f9a750d966ab52545d877986 (diff) |
fallback using toFixed() if both Intl.NumberFormat() and Number.toLocaleString() give wrong results; #2103
Diffstat (limited to 'web')
-rw-r--r-- | web/dashboard.js | 68 | ||||
-rw-r--r-- | web/index.html | 2 |
2 files changed, 65 insertions, 5 deletions
diff --git a/web/dashboard.js b/web/dashboard.js index 1073efcdb5..5fe189f8ba 100644 --- a/web/dashboard.js +++ b/web/dashboard.js @@ -734,13 +734,13 @@ var NETDATA = window.NETDATA || {}; }; // ---------------------------------------------------------------------------------------------------------------- - // fast numbers formating + // fast numbers formatting NETDATA.fastNumberFormat = { - numberFormatWorks: undefined, formatters_fixed: [], formatters_zero_based: [], + // this is the fastest and the preferred getIntlNumberFormat: function(min, max) { var key = max; if(min == max) { @@ -786,6 +786,7 @@ var NETDATA = window.NETDATA || {}; } }, + // this respects locale getLocaleString: function(min, max) { var key = max; if(min == max) { @@ -841,6 +842,40 @@ var NETDATA = window.NETDATA || {}; } }, + getFixed: function(min, max) { + var key = max; + if(min == max) { + if(typeof this.formatters_fixed[key] == 'undefined') + this.formatters_fixed[key] = { + format: function (value) { + if(value === 0) return "0"; + return value.toFixed(max); + } + }; + + return this.formatters_fixed[key]; + } + else if(min == 0) { + if(typeof this.formatters_zero_based[key] == 'undefined') + this.formatters_zero_based[key] = { + format: function (value) { + if(value === 0) return "0"; + return value.toFixed(max); + } + }; + + return this.formatters_zero_based[key]; + } + else { + return { + format: function (value) { + if(value === 0) return "0"; + return value.toFixed(max); + } + }; + } + }, + testIntlNumberFormat: function() { var n = 1.12345; var e1 = "1.12", e2 = "1,12"; @@ -859,19 +894,44 @@ var NETDATA = window.NETDATA || {}; s = ""; } - console.log(s); + console.log('NumberFormat: ', s); return (s == e1 || s == e2); }, + testLocaleString: function() { + var n = 1.12345; + var e1 = "1.12", e2 = "1,12"; + var s = ""; + + try { + s = value.toLocaleString(undefined, { + useGrouping: true, + minimumFractionDigits: 2, + maximumFractionDigits: 2 + }); + } + catch(e) { + s = ""; + } + + console.log('localeString: ', s); + return (s == e1 || s == e2); + }, + + // on first run we decide which formatter to use get: function(min, max) { if(this.testIntlNumberFormat()) { console.log('numberformat'); this.get = this.getIntlNumberFormat; } - else { + else if(this.testLocaleString()) { console.log('localestring'); this.get = this.getLocaleString; } + else { + console.log('fixed'); + this.get = this.getFixed; + } return this.get(min, max); } }; diff --git a/web/index.html b/web/index.html index ad6a646c5a..0c8fbb0a2c 100644 --- a/web/index.html +++ b/web/index.html @@ -3523,4 +3523,4 @@ </div> </body> </html> -<script type="text/javascript" src="dashboard.js?v20170419-3"></script> +<script type="text/javascript" src="dashboard.js?v20170419-5"></script> |