fallback using toFixed() if both Intl.NumberFormat() and Number.toLocaleString() give wrong results; #2103
Costa Tsaousis (ktsaou) committed
Apr 19, 2017 at 22:52 UTC
54c53020c3e0ffd9ddd05b40743c0781cff3dd8f
2 files changed
+65
-5
web/dashboard.js
+64
-4
@@ -734,13 +734,13 @@ var NETDATA = window.NETDATA || {};
734
};
735
736
// ----------------------------------------------------------------------------------------------------------------
737
- // fast numbers formating
737
+ // fast numbers formatting
738
739
NETDATA.fastNumberFormat = {
740
- numberFormatWorks: undefined,
740
formatters_fixed: [],
741
formatters_zero_based: [],
742
743
+ // this is the fastest and the preferred
744
getIntlNumberFormat: function(min, max) {
745
var key = max;
746
if(min == max) {
@@ -786,6 +786,7 @@ var NETDATA = window.NETDATA || {};
786
}
787
},
788
789
+ // this respects locale
790
getLocaleString: function(min, max) {
791
var key = max;
792
if(min == max) {
@@ -841,6 +842,40 @@ var NETDATA = window.NETDATA || {};
842
}
843
},
844
845
+ getFixed: function(min, max) {
846
+ var key = max;
847
+ if(min == max) {
848
+ if(typeof this.formatters_fixed[key] == 'undefined')
849
+ this.formatters_fixed[key] = {
850
+ format: function (value) {
851
+ if(value === 0) return "0";
852
+ return value.toFixed(max);
853
+ }
854
+ };
855
+
856
+ return this.formatters_fixed[key];
857
+ }
858
+ else if(min == 0) {
859
+ if(typeof this.formatters_zero_based[key] == 'undefined')
860
+ this.formatters_zero_based[key] = {
861
+ format: function (value) {
862
+ if(value === 0) return "0";
863
+ return value.toFixed(max);
864
+ }
865
+ };
866
+
867
+ return this.formatters_zero_based[key];
868
+ }
869
+ else {
870
+ return {
871
+ format: function (value) {
872
+ if(value === 0) return "0";
873
+ return value.toFixed(max);
874
+ }
875
+ };
876
+ }
877
+ },
878
+
879
testIntlNumberFormat: function() {
880
var n = 1.12345;
881
var e1 = "1.12", e2 = "1,12";
@@ -859,19 +894,44 @@ var NETDATA = window.NETDATA || {};
894
s = "";
895
}
896
862
- console.log(s);
897
+ console.log('NumberFormat: ', s);
898
return (s == e1 || s == e2);
899
},
900
901
+ testLocaleString: function() {
902
+ var n = 1.12345;
903
+ var e1 = "1.12", e2 = "1,12";
904
+ var s = "";
905
+
906
+ try {
907
+ s = value.toLocaleString(undefined, {
908
+ useGrouping: true,
909
+ minimumFractionDigits: 2,
910
+ maximumFractionDigits: 2
911
+ });
912
+ }
913
+ catch(e) {
914
+ s = "";
915
+ }
916
+
917
+ console.log('localeString: ', s);
918
+ return (s == e1 || s == e2);
919
+ },
920
+
921
+ // on first run we decide which formatter to use
922
get: function(min, max) {
923
if(this.testIntlNumberFormat()) {
924
console.log('numberformat');
925
this.get = this.getIntlNumberFormat;
926
}
871
- else {
927
+ else if(this.testLocaleString()) {
928
console.log('localestring');
929
this.get = this.getLocaleString;
930
}
931
+ else {
932
+ console.log('fixed');
933
+ this.get = this.getFixed;
934
+ }
935
return this.get(min, max);
936
}
937
};
web/index.html
+1
-1
@@ -3523,4 +3523,4 @@
3523
</div>
3524
</body>
3525
</html>
3526
-<script type="text/javascript" src="dashboard.js?v20170419-3"></script>
3526
+<script type="text/javascript" src="dashboard.js?v20170419-5"></script>