Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(sheet): cell precision error (#4476)
Dushusir99b2600b1591
diff --git a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.tsindex d491872287..8d97ce542e 100644--- a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts+++ b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts@@ -16,7 +16,7 @@import type { ICommandInfo } from '@univerjs/core';import type { ISetArrayFormulaDataMutationParams } from '@univerjs/engine-formula';-import { CellValueType, Disposable, ICommandService, Inject, InterceptorEffectEnum } from '@univerjs/core';+import { CellValueType, Disposable, ICommandService, Inject, InterceptorEffectEnum, isRealNum } from '@univerjs/core';import { FormulaDataModel, SetArrayFormulaDataMutation, stripErrorMargin } from '@univerjs/engine-formula';import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';@@ -81,10 +81,14 @@ export class ArrayFormulaCellInterceptorController extends Disposable {}// Dealing with precision issues- if (cell?.t === CellValueType.NUMBER && typeof cell?.v === 'number') {+ // Need to be compatible with the case where v is a string but the cell type is a number+ // e.g.+ // "v": "123413.23000000001",+ // "t": 2,+ if (cell?.t === CellValueType.NUMBER && cell.v !== undefined && cell.v !== null && isRealNum(cell.v)) {return next({...cell,- v: stripErrorMargin(cell.v),+ v: stripErrorMargin(Number(cell.v)),});}diff --git a/packages/sheets-numfmt/package.json b/packages/sheets-numfmt/package.jsonindex 1cd728ec88..c032031041 100644--- a/packages/sheets-numfmt/package.json+++ b/packages/sheets-numfmt/package.json@@ -68,6 +68,7 @@},"dependencies": {"@univerjs/core": "workspace:*",+ "@univerjs/engine-formula": "workspace:*","@univerjs/engine-numfmt": "workspace:*","@univerjs/sheets": "workspace:*"},diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.tsindex c3fa9c2169..681ebfdd5a 100644--- a/packages/sheets-numfmt/src/utils/pattern.ts+++ b/packages/sheets-numfmt/src/utils/pattern.ts@@ -17,6 +17,7 @@import type { INumfmtLocalTag } from '@univerjs/core';import type { FormatType } from '@univerjs/sheets';import { numfmt } from '@univerjs/core';+import { stripErrorMargin } from '@univerjs/engine-formula';export const getPatternType = (pattern: string): FormatType => numfmt.getInfo(pattern).type || 'unknown';interface IPatternPreview {@@ -42,7 +43,7 @@ export const getPatternPreview = (pattern: string, value: number, locale: INumfmexport const getPatternPreviewIgnoreGeneral = (pattern: string, value: number, locale?: INumfmtLocalTag): IPatternPreview => {if (pattern === 'General') {return {- result: String(value),+ result: String(stripErrorMargin(value)), // In Excel, the default General format also needs to handle numeric precision.};}return getPatternPreview(pattern, value, locale);diff --git a/packages/sheets/src/controllers/number-cell.controller.ts b/packages/sheets/src/controllers/number-cell.controller.tsindex dd98180d09..257df57458 100644--- a/packages/sheets/src/controllers/number-cell.controller.ts+++ b/packages/sheets/src/controllers/number-cell.controller.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import { CellValueType, Disposable, Inject, InterceptorEffectEnum } from '@univerjs/core';+import { CellValueType, Disposable, Inject, InterceptorEffectEnum, isRealNum } from '@univerjs/core';import { stripErrorMargin } from '@univerjs/engine-formula';import { INTERCEPTOR_POINT } from '../services/sheet-interceptor/interceptor-const';import { SheetInterceptorService } from '../services/sheet-interceptor/sheet-interceptor.service';@@ -44,10 +44,14 @@ export class NumberCellDisplayController extends Disposable {}// Dealing with precision issues- if (cell?.t === CellValueType.NUMBER && typeof cell?.v === 'number') {+ // Need to be compatible with the case where v is a string but the cell type is a number+ // e.g.+ // "v": "123413.23000000001",+ // "t": 2,+ if (cell?.t === CellValueType.NUMBER && cell.v !== undefined && cell.v !== null && isRealNum(cell.v)) {return next({...cell,- v: stripErrorMargin(cell.v),+ v: stripErrorMargin(Number(cell.v)),});}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 099b500b58..b9301347d4 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -2783,6 +2783,9 @@ importers:'@univerjs/core':specifier: workspace:*version: link:../core+ '@univerjs/engine-formula':+ specifier: workspace:*+ version: link:../engine-formula'@univerjs/engine-numfmt':specifier: workspace:*version: link:../engine-numfmt