машинная ошибка
This commit is contained in:
@@ -13,6 +13,15 @@ const QUALIFIED_RANGE_RE = /\b[A-Za-z0-9_]+![A-Z]+[0-9]+:[A-Z]+[0-9]+\b/g // She
|
|||||||
// Типы для значений ячеек
|
// Типы для значений ячеек
|
||||||
export type CellValue = string | number | boolean | null | undefined
|
export type CellValue = string | number | boolean | null | undefined
|
||||||
|
|
||||||
|
// Утилитная функция для устранения машинной ошибки в числах с плавающей точкой
|
||||||
|
function fixFloatingPointError(value: CellValue): CellValue {
|
||||||
|
if (typeof value === 'number' && isFinite(value)) {
|
||||||
|
const factor = Math.pow(10, 14)
|
||||||
|
return Math.round(value * factor) / factor
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
export class Cell {
|
export class Cell {
|
||||||
sheet: Sheet
|
sheet: Sheet
|
||||||
name: string
|
name: string
|
||||||
@@ -248,7 +257,10 @@ export class Cell {
|
|||||||
// Создаём функцию с контекстом
|
// Создаём функцию с контекстом
|
||||||
const func = new Function(...Object.keys(context), `return ${expr}`)
|
const func = new Function(...Object.keys(context), `return ${expr}`)
|
||||||
|
|
||||||
return func(...Object.values(context))
|
const result = func(...Object.values(context))
|
||||||
|
|
||||||
|
// Исправляем машинную ошибку в 16-м символе после запятой
|
||||||
|
return fixFloatingPointError(result)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Invalid expression: ${expr}`)
|
throw new Error(`Invalid expression: ${expr}`)
|
||||||
}
|
}
|
||||||
@@ -430,7 +442,10 @@ export class Workbook {
|
|||||||
if (this._dirty.has(qualifiedName)) {
|
if (this._dirty.has(qualifiedName)) {
|
||||||
this.recalc()
|
this.recalc()
|
||||||
}
|
}
|
||||||
return this.getCell(qualifiedName).value
|
const value = this.getCell(qualifiedName).value
|
||||||
|
|
||||||
|
// Исправляем машинную ошибку в 16-м символе после запятой для числовых значений
|
||||||
|
return fixFloatingPointError(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Управление волатильными ячейками
|
// Управление волатильными ячейками
|
||||||
|
|||||||
Reference in New Issue
Block a user