Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

excel 展示数字的时候是否可以改成可配置的 去 toFixed(1) 保留几位小数 #254

Open
wyaoting opened this issue Mar 22, 2024 · 0 comments

Comments

@wyaoting
Copy link

 if (cell.style.numFmt) {
          if (cell.style.numFmt.endsWith("%")) {
            let precision = cell.style.numFmt.match(/\.(\d+)%/);
            if (precision) {
              return (value * 100).toFixed(precision[1].length) + "%";
            } else {
              return value * 100 + "%";
            }
          } else if (/0(\.0+)?/.test(cell.style.numFmt)) {
            let prefix = "";
            if (cell.style.numFmt.startsWith("$")) {
              prefix = "$";
            } else if (cell.style.numFmt.startsWith('"¥')) {
              prefix = "¥";
            }

            if (value === 0 && cell.style.numFmt.startsWith("_")) {
              return "-";
            }
            let precision = cell.style.numFmt.match(/0\.(0+)(_|;|$)/);
            if (precision) {
              precision = precision[1].length;
            } else {
              precision = 0;
            }

            let result = value.toFixed(precision) + "";
            if (cell.style.numFmt.includes("#,##")) {
              //千分位
              result = result.split(".");
              let number = result[0].split("").reverse();
              let newNumber = [];
              for (let i = 0; i < number.length; i++) {
                newNumber.push(number[i]);
                if (
                  (i + 1) % 3 === 0 &&
                  i < number.length - 1 &&
                  number[i + 1] !== "-"
                ) {
                  newNumber.push(",");
                }
              }
              result[0] = newNumber.reverse().join("");
              result = result.join(".");
            }
            return prefix + result;
          }
        }
        return value.toFixed(1) + "";

image
大佬有时间优化吗?
x-data-spreadsheet 这个库不知道为啥会自动 聚焦,github 库上有 autoFocus 但是我看大佬依赖包没有这个配置项

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant