소스 검색

Retain decimal precision when exporting CSV

Using `Number.prototype.toLocaleString()` has the unexpected behavior of truncating anything
exceeding 3 decimal digits on floats.

Additionally, it introduces inconsistencies (comma vs period separators) which could make processing the
output CSV harder than it could be.  The proposed solution here is to simply let numbers be cast
automatically via string concatenation.

Fixes #13929
Victor Cinaglia 7 년 전
부모
커밋
4ed0a3d29a
2개의 변경된 파일3개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 0
      public/app/core/specs/file_export.test.ts
  2. 1 3
      public/app/core/utils/file_export.ts

+ 2 - 0
public/app/core/specs/file_export.test.ts

@@ -73,6 +73,7 @@ describe('file_export', () => {
         ],
         ],
         rows: [
         rows: [
           [123, 'some_string', 1.234, true],
           [123, 'some_string', 1.234, true],
+          [1000, 'some_string', 1.234567891, true],
           [0o765, 'some string with " in the middle', 1e-2, false],
           [0o765, 'some string with " in the middle', 1e-2, false],
           [0o765, 'some string with "" in the middle', 1e-2, false],
           [0o765, 'some string with "" in the middle', 1e-2, false],
           [0o765, 'some string with """ in the middle', 1e-2, false],
           [0o765, 'some string with """ in the middle', 1e-2, false],
@@ -89,6 +90,7 @@ describe('file_export', () => {
       const expectedText =
       const expectedText =
         '"integer_value";"string_value";"float_value";"boolean_value"\r\n' +
         '"integer_value";"string_value";"float_value";"boolean_value"\r\n' +
         '123;"some_string";1.234;true\r\n' +
         '123;"some_string";1.234;true\r\n' +
+        '1000;"some_string";1.234567891;true\r\n' +
         '501;"some string with "" in the middle";0.01;false\r\n' +
         '501;"some string with "" in the middle";0.01;false\r\n' +
         '501;"some string with """" in the middle";0.01;false\r\n' +
         '501;"some string with """" in the middle";0.01;false\r\n' +
         '501;"some string with """""" in the middle";0.01;false\r\n' +
         '501;"some string with """""" in the middle";0.01;false\r\n' +

+ 1 - 3
public/app/core/utils/file_export.ts

@@ -41,10 +41,8 @@ function formatSpecialHeader(useExcelHeader) {
 function formatRow(row, addEndRowDelimiter = true) {
 function formatRow(row, addEndRowDelimiter = true) {
   let text = '';
   let text = '';
   for (let i = 0; i < row.length; i += 1) {
   for (let i = 0; i < row.length; i += 1) {
-    if (isBoolean(row[i]) || isNullOrUndefined(row[i])) {
+    if (isBoolean(row[i]) || isNumber(row[i]) || isNullOrUndefined(row[i])) {
       text += row[i];
       text += row[i];
-    } else if (isNumber(row[i])) {
-      text += row[i].toLocaleString();
     } else {
     } else {
       text += `${QUOTE}${csvEscaped(htmlUnescaped(htmlDecoded(row[i])))}${QUOTE}`;
       text += `${QUOTE}${csvEscaped(htmlUnescaped(htmlDecoded(row[i])))}${QUOTE}`;
     }
     }