From d4ebb2b14e2b53f318c1d45780aea3e2bfb3d86f Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 20 十月 2026 13:20:46 +0800
Subject: [PATCH] 修复科学计数法格式顺序
---
src/utils/inspectionValueFormat.js | 29 ++++++++++++++++++++++-------
1 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/src/utils/inspectionValueFormat.js b/src/utils/inspectionValueFormat.js
index f0f536a..f9b6b16 100644
--- a/src/utils/inspectionValueFormat.js
+++ b/src/utils/inspectionValueFormat.js
@@ -12,11 +12,13 @@
.split(',')
.map(item => item.trim())
.filter(Boolean);
- const decimalPlaces = parts[1] === '' || parts[1] == null ? null : Number(parts[1]);
+ const decimalIndex = parts.findIndex((item, index) => index > 0 && /^\d+$/.test(item));
+ const decimalPlaces = decimalIndex < 0 ? null : Number(parts[decimalIndex]);
if (decimalPlaces != null && (!Number.isInteger(decimalPlaces) || decimalPlaces < 0 || decimalPlaces > 15)) {
return { error: '灏忔暟浣嶆暟蹇呴』鏄� 0 鍒� 15 鐨勬暣鏁�' };
}
- const displayMode = parts.slice(2).includes('绉戝璁℃暟娉�') ? 'SCIENTIFIC' : 'NORMAL';
+ const scientificIndex = parts.findIndex((item, index) => index > 0 && item === '绉戝璁℃暟娉�');
+ const displayMode = scientificIndex >= 0 ? 'SCIENTIFIC' : 'NORMAL';
const roundingNames = {
'鍥涜垗浜斿叆': 'HALF_UP',
'鍥涜垗鍏叆': 'HALF_UP_6',
@@ -25,17 +27,30 @@
'鎴柇': 'DOWN'
};
const roundingPart = parts.slice(2).find(item => Object.prototype.hasOwnProperty.call(roundingNames, item));
- const unsupported = parts.slice(2).filter(item => item && item !== '绉戝璁℃暟娉�' && !roundingNames[item]);
+ const unsupported = parts.slice(1).filter((item, index) =>
+ item && index + 1 !== decimalIndex && item !== '绉戝璁℃暟娉�' && !roundingNames[item]
+ );
if (unsupported.length || parts.length > 4) {
return { error: '妫�楠屽�兼壒娉ㄦ牸寮忓簲涓猴細妫�楠屽�硷紝2锛屽洓鑸嶄簲鍏� 鎴� 妫�楠屽�硷紝2锛岀瀛﹁鏁版硶锛屽洓鑸嶄簲鍏�' };
}
- return { decimalPlaces, displayMode, roundingMode: roundingPart ? roundingNames[roundingPart] : 'HALF_UP' };
+ return {
+ decimalPlaces,
+ displayMode,
+ // 鍙傛暟椤哄簭鍐冲畾澶勭悊椤哄簭锛氱瀛﹁鏁版硶鍦ㄥ皬鏁颁綅鍓嶈〃绀哄厛杞瀛﹁鏁版硶銆�
+ scientificFirst: displayMode === 'SCIENTIFIC' && scientificIndex < decimalIndex,
+ roundingMode: roundingPart ? roundingNames[roundingPart] : 'HALF_UP'
+ };
}
export function formatInspectionValue(value, config) {
if (!config || config.error || config.decimalPlaces == null || value === '' || value == null) return value;
const number = Number(value);
if (!Number.isFinite(number)) return value;
+ // 绉戝璁℃暟娉曞湪鎵规敞涓帓鍦ㄥ皬鏁颁綅鏁板墠鏃讹紝鍏堣浆绉戝璁℃暟娉曞啀淇濈暀灏炬暟浣嶆暟銆�
+ if (config.displayMode === 'SCIENTIFIC' && config.scientificFirst) {
+ const scientific = number.toExponential(config.decimalPlaces);
+ return scientific.replace('e', 'E');
+ }
const factor = 10 ** config.decimalPlaces;
let rounded;
if (config.roundingMode === 'CEILING') rounded = Math.ceil(number * factor) / factor;
@@ -47,9 +62,9 @@
const fraction = absolute - base;
rounded = (number < 0 ? -1 : 1) * ((fraction >= 0.6 ? base + 1 : base) / factor);
} else rounded = Math.round((number + (number >= 0 ? Number.EPSILON : -Number.EPSILON)) * factor) / factor;
- return config.displayMode === 'SCIENTIFIC'
- ? rounded.toExponential(config.decimalPlaces)
- : rounded.toFixed(config.decimalPlaces);
+ const fixed = rounded.toFixed(config.decimalPlaces);
+ if (config.displayMode === 'SCIENTIFIC') return Number(fixed).toExponential(config.decimalPlaces).replace('e', 'E');
+ return fixed;
}
export function validateInspectionValueAnnotation(annotation) {
--
Gitblit v1.9.3