import { Components } from '@formio/js'; const DEFAULT_CALCULATED_DECIMAL_LIMIT = 10; interface NumberInstance { component: { calculateValue?: unknown; calculateValueVariable?: unknown; decimalLimit?: unknown; validate?: { step?: unknown }; }; decimalLimit?: number; decimalSeparator?: string; } interface NumberPrototype { __gdsCalculatedNumberRoundingInstalled?: boolean; doValueCalculation: (this: NumberInstance, ...args: unknown[]) => unknown; parseValue: (this: NumberInstance, input: unknown) => string | null; getMaskedValue: (this: NumberInstance, value: unknown) => string; } interface NumberConstructor { prototype: NumberPrototype; } const decimalLimit = (instance: NumberInstance): number => { if (typeof instance.component.decimalLimit === 'number') { return instance.component.decimalLimit; } const step = instance.component.validate?.step; if (typeof step === 'number' || typeof step === 'string') { const [, decimals = ''] = String(step).split('.'); if (decimals) return decimals.length; } return DEFAULT_CALCULATED_DECIMAL_LIMIT; }; const toPlainDecimal = (value: number, places: number): string => new Intl.NumberFormat('en-US', { useGrouping: false, maximumFractionDigits: places, }).format(Object.is(value, -0) ? 0 : value); const roundedValue = (value: unknown, places: number): unknown => { if (typeof value !== 'number' || !Number.isFinite(value)) return value; const rounded = Number(toPlainDecimal(value, places)); return Object.is(rounded, -0) ? 0 : rounded; }; /** * Rounds calculated Number and Currency values before Form.io stores them and * keeps scientific input and text representations out of exponent notation. */ export const installCalculatedNumberRounding = ( NumberClass?: NumberConstructor, ): void => { const resolvedClass = NumberClass ?? (Components.components.number as unknown as NumberConstructor); const prototype = resolvedClass.prototype; if (Object.prototype.hasOwnProperty.call(prototype, '__gdsCalculatedNumberRoundingInstalled')) return; prototype.__gdsCalculatedNumberRoundingInstalled = true; const originalCalculate = prototype.doValueCalculation; const originalParseValue = prototype.parseValue; const originalGetMaskedValue = prototype.getMaskedValue; prototype.doValueCalculation = function (this: NumberInstance, ...args: unknown[]): unknown { const value = originalCalculate.call(this, ...args); return roundedValue(value, decimalLimit(this)); }; prototype.parseValue = function (this: NumberInstance, input: unknown): string | null { if (!/[eE]/.test(String(input))) return originalParseValue.call(this, input) as string | null; const value = Number(input); if (!Number.isFinite(value)) return originalParseValue.call(this, input) as string | null; return toPlainDecimal(value, decimalLimit(this)).replace('.', this.decimalSeparator ?? '.'); }; prototype.getMaskedValue = function (this: NumberInstance, value: unknown): string { if (typeof value === 'number' && Number.isFinite(value)) { value = toPlainDecimal(value, decimalLimit(this)); } return originalGetMaskedValue.call(this, value); }; };