resolver-egretimp-plus 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resolver-egretimp-plus",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "交付体验渲染",
5
5
  "main": "./dist/web/index.js",
6
6
  "module": "./dist/web/index.js",
@@ -1,6 +1,6 @@
1
1
  import { ElInput } from 'element-plus'
2
2
  import { computed, defineProps, useAttrs } from 'vue'
3
- import { commonPropsType, hasOwn, isElement } from '../../utils/index.js'
3
+ import { commonPropsType, hasOwn, isElement, parseFloatToString } from '../../utils/index.js'
4
4
  import { ref } from 'vue';
5
5
  import { onMounted } from 'vue';
6
6
  import { getDomWidth } from '../../utils/dom.js';
@@ -50,7 +50,7 @@ export default {
50
50
  default: 2
51
51
  },
52
52
  // 金额显示时保留小数点0后缀
53
- decimalSuffix: {
53
+ keepLastZero: {
54
54
  type: [Number, String],
55
55
  default: '0'
56
56
  },
@@ -246,13 +246,12 @@ export default {
246
246
 
247
247
  // 转换数字并限制范围
248
248
  val = Math.max(props.min, Math.min(props.max, val))
249
-
250
249
  // 处理千分位
251
250
  if (props.decimal != '-1') {
252
251
  val = val.toFixed(props.decimal)
253
252
  }
254
- if (props.decimalSuffix != '1') {
255
- val = parseFloat(val)
253
+ if (props.keepLastZero != '1') {
254
+ val = parseFloatToString(val)
256
255
  }
257
256
  const parts = `${val}`.split('.')
258
257
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, props.moneySeg)
@@ -285,7 +284,7 @@ export default {
285
284
  return ''
286
285
  })
287
286
  const parsed = Number(repVal)
288
- return isNaN(parsed) ? '' : parsed
287
+ return isNaN(parsed) ? '' : parseFloatToString(parsed)
289
288
  }
290
289
 
291
290
  expose({
@@ -484,4 +484,85 @@ export function createEmptyCopy(source, cache = new WeakMap()) {
484
484
  }
485
485
 
486
486
  return copy;
487
+ }
488
+
489
+ export function parseFloatToString(input) {
490
+ // 1. 处理输入类型
491
+ if (input === null || input === undefined) return "NaN";
492
+ const str = String(input).trim();
493
+
494
+ // 2. 匹配浮点数(包括科学计数法)
495
+ const floatRegex = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/;
496
+ const match = str.match(floatRegex);
497
+ if (!match) return "NaN";
498
+
499
+ // 3. 处理科学计数法
500
+ let numberStr = match[0];
501
+ if (/e/i.test(numberStr)) {
502
+ numberStr = convertScientificNotation(numberStr);
503
+ }
504
+
505
+ // 4. 规范化结果(去除多余的前导/后缀零)
506
+ return normalizeNumberString(numberStr);
507
+ }
508
+
509
+ // 科学计数法转普通数字字符串
510
+ function convertScientificNotation(sciStr) {
511
+ const [base, expPart] = sciStr.split(/e/gi);
512
+ const exponent = expPart ? parseInt(expPart, 10) : 0;
513
+
514
+ let [integer, decimal = ""] = base.split(".");
515
+ const isNegative = integer.startsWith("-");
516
+ if (isNegative) integer = integer.slice(1);
517
+
518
+ // 合并整数和小数部分
519
+ let numStr = integer + decimal;
520
+ const decimalPos = integer.length;
521
+
522
+ // 计算新小数点位置
523
+ const newPos = decimalPos + exponent;
524
+
525
+ // 处理不同情况
526
+ if (newPos >= numStr.length) {
527
+ // 右移超出:补零
528
+ return (isNegative ? "-" : "") + numStr + "0".repeat(newPos - numStr.length);
529
+ } else if (newPos <= 0) {
530
+ // 左移超出:补零
531
+ const zeros = "0".repeat(-newPos);
532
+ return (isNegative ? "-" : "") + "0." + zeros + numStr;
533
+ } else {
534
+ // 插入小数点
535
+ return (isNegative ? "-" : "") +
536
+ numStr.slice(0, newPos) +
537
+ (numStr.slice(newPos) ? "." + numStr.slice(newPos) : "");
538
+ }
539
+ }
540
+
541
+ // 规范化数字字符串
542
+ function normalizeNumberString(numStr) {
543
+ // 处理空字符串
544
+ if (numStr === "") return "0";
545
+
546
+ // 分解符号、整数和小数部分
547
+ const isNegative = numStr.startsWith("-");
548
+ const cleanStr = isNegative ? numStr.slice(1) : numStr;
549
+ let [integer, decimal] = cleanStr.split(".");
550
+
551
+ // 处理整数部分前导零
552
+ integer = integer.replace(/^0+/, "") || "0";
553
+
554
+ // 处理小数部分后缀零
555
+ decimal = (decimal || "").replace(/0+$/, "");
556
+
557
+ // 组合结果
558
+ let result = "";
559
+ result += isNegative ? "-" : "";
560
+ result += integer;
561
+ result += decimal ? "." + decimal : "";
562
+
563
+ // 特殊处理 ".5" -> "0.5"
564
+ if (result.startsWith(".")) result = "0" + result;
565
+ if (result.startsWith("-.")) result = "-0" + result.slice(1);
566
+
567
+ return result;
487
568
  }