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
feat(formula): supplement ROUNDBANK formula (#4013)
wpxp1234567c3807fc7591
diff --git a/packages/engine-formula/src/functions/math/function-map.ts b/packages/engine-formula/src/functions/math/function-map.tsindex 74c71d1016..e1d56700f0 100644--- a/packages/engine-formula/src/functions/math/function-map.ts+++ b/packages/engine-formula/src/functions/math/function-map.ts@@ -71,6 +71,7 @@ import { Randarray } from './randarray';import { Randbetween } from './randbetween';import { Roman } from './roman';import { Round } from './round';+import { Roundbank } from './roundbank';import { Rounddown } from './rounddown';import { Roundup } from './roundup';import { Sec } from './sec';@@ -152,6 +153,7 @@ export const functionMath = [[Randbetween, FUNCTION_NAMES_MATH.RANDBETWEEN],[Roman, FUNCTION_NAMES_MATH.ROMAN],[Round, FUNCTION_NAMES_MATH.ROUND],+ [Roundbank, FUNCTION_NAMES_MATH.ROUNDBANK],[Rounddown, FUNCTION_NAMES_MATH.ROUNDDOWN],[Roundup, FUNCTION_NAMES_MATH.ROUNDUP],[Sec, FUNCTION_NAMES_MATH.SEC],diff --git a/packages/engine-formula/src/functions/math/function-names.ts b/packages/engine-formula/src/functions/math/function-names.tsindex f1f8a308e4..5155d31256 100644--- a/packages/engine-formula/src/functions/math/function-names.ts+++ b/packages/engine-formula/src/functions/math/function-names.ts@@ -74,6 +74,7 @@ export enum FUNCTION_NAMES_MATH {RANDBETWEEN = 'RANDBETWEEN',ROMAN = 'ROMAN',ROUND = 'ROUND',+ ROUNDBANK = 'ROUNDBANK',ROUNDDOWN = 'ROUNDDOWN',ROUNDUP = 'ROUNDUP',SEC = 'SEC',diff --git a/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.tsnew file mode 100644index 0000000000..fa831a5f58--- /dev/null+++ b/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts@@ -0,0 +1,150 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';++import { ErrorType } from '../../../../basics/error-type';+import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';+import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';+import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';+import { FUNCTION_NAMES_MATH } from '../../function-names';+import { Roundbank } from '../index';++describe('Test roundbank function', () => {+ const testFunction = new Roundbank(FUNCTION_NAMES_MATH.ROUNDBANK);++ describe('Roundbank', () => {+ it('Value is normal', () => {+ const number = NumberValueObject.create(2.344);+ const numDigits = NumberValueObject.create(2);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(2.34);++ const number2 = NumberValueObject.create(2.346);+ const result2 = testFunction.calculate(number2, numDigits);+ expect(result2.getValue()).toBe(2.35);++ const number3 = NumberValueObject.create(2.345);+ const result3 = testFunction.calculate(number3, numDigits);+ expect(result3.getValue()).toBe(2.34);++ const number4 = NumberValueObject.create(2.3451);+ const result4 = testFunction.calculate(number4, numDigits);+ expect(result4.getValue()).toBe(2.35);+ });++ it('Number value is negtive', () => {+ const number = NumberValueObject.create(-2.344);+ const numDigits = NumberValueObject.create(2);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(-2.34);++ const number2 = NumberValueObject.create(-2.346);+ const result2 = testFunction.calculate(number2, numDigits);+ expect(result2.getValue()).toBe(-2.35);++ const number3 = NumberValueObject.create(-2.345);+ const result3 = testFunction.calculate(number3, numDigits);+ expect(result3.getValue()).toBe(-2.34);++ const number4 = NumberValueObject.create(-2.3451);+ const result4 = testFunction.calculate(number4, numDigits);+ expect(result4.getValue()).toBe(-2.35);+ });++ it('NumDigits value is negtive', () => {+ const number = NumberValueObject.create(2.344);+ const numDigits = NumberValueObject.create(-2);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(0);++ const number2 = NumberValueObject.create(-222.346);+ const result2 = testFunction.calculate(number2, numDigits);+ expect(result2.getValue()).toBe(-200);++ const number3 = NumberValueObject.create(22.345);+ const numDigits2 = NumberValueObject.create(-1);+ const result3 = testFunction.calculate(number3, numDigits2);+ expect(result3.getValue()).toBe(20);++ const number4 = NumberValueObject.create(-222.3451);+ const result4 = testFunction.calculate(number4, numDigits2);+ expect(result4.getValue()).toBe(-220);+ });++ it('Value is normal string', () => {+ const number = StringValueObject.create('test');+ const numDigits = NumberValueObject.create(1);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(ErrorType.VALUE);+ });++ it('Value is boolean', () => {+ const number = NumberValueObject.create(2.15);+ const numDigits = BooleanValueObject.create(true);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(2.2);+ });++ it('Value is blank cell', () => {+ const number = NumberValueObject.create(2.15);+ const numDigits = NullValueObject.create();+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(2);+ });++ it('Value is error', () => {+ const number = ErrorValueObject.create(ErrorType.NAME);+ const numDigits = NumberValueObject.create(1);+ const result = testFunction.calculate(number, numDigits);+ expect(result.getValue()).toBe(ErrorType.NAME);++ const number2 = NumberValueObject.create(2.15);+ const numDigits2 = ErrorValueObject.create(ErrorType.NAME);+ const result2 = testFunction.calculate(number2, numDigits2);+ expect(result2.getValue()).toBe(ErrorType.NAME);+ });++ it('Value is array and array', () => {+ const number = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [2.15, 2.149, -1.575, '21.5', 626.3, 1.98, '-50.55'],+ ]),+ rowCount: 1,+ columnCount: 7,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const numDigits = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [1, true, 2, false, -1, -2.5, -3, null, ' '],+ ]),+ rowCount: 1,+ columnCount: 9,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result = testFunction.calculate(number, numDigits);+ expect(transformToValue(result.getArrayValue())).toStrictEqual([+ [2.2, 2.1, -1.58, 22, 630, 0, -0, ErrorType.NA, ErrorType.NA],+ ]);+ });+ });+});diff --git a/packages/engine-formula/src/functions/math/roundbank/index.ts b/packages/engine-formula/src/functions/math/roundbank/index.tsnew file mode 100644index 0000000000..c10e7f156d--- /dev/null+++ b/packages/engine-formula/src/functions/math/roundbank/index.ts@@ -0,0 +1,98 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';+import { ErrorType } from '../../../basics/error-type';+import { expandArrayValueObject } from '../../../engine/utils/array-object';+import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';+import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';+import { NumberValueObject } from '../../../engine/value-object/primitive-object';+import { BaseFunction } from '../../base-function';++export class Roundbank extends BaseFunction {+ override minParams = 2;++ override maxParams = 2;++ override calculate(number: BaseValueObject, numDigits: BaseValueObject): BaseValueObject {+ const maxRowLength = Math.max(+ number.isArray() ? (number as ArrayValueObject).getRowCount() : 1,+ numDigits.isArray() ? (numDigits as ArrayValueObject).getRowCount() : 1+ );++ const maxColumnLength = Math.max(+ number.isArray() ? (number as ArrayValueObject).getColumnCount() : 1,+ numDigits.isArray() ? (numDigits as ArrayValueObject).getColumnCount() : 1+ );++ const numberArray = expandArrayValueObject(maxRowLength, maxColumnLength, number, ErrorValueObject.create(ErrorType.NA));+ const numDigitsArray = expandArrayValueObject(maxRowLength, maxColumnLength, numDigits, ErrorValueObject.create(ErrorType.NA));++ const resultArray = numberArray.mapValue((numberObject, rowIndex, columnIndex) => {+ const numDigitsObject = numDigitsArray.get(rowIndex, columnIndex) as BaseValueObject;++ if (numberObject.isError()) {+ return numberObject;+ }++ if (numDigitsObject.isError()) {+ return numDigitsObject;+ }++ const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(numberObject, numDigitsObject);++ if (isError) {+ return errorObject as ErrorValueObject;+ }++ const [_numberObject, _numDigitsObject] = variants as BaseValueObject[];++ const numberValue = +_numberObject.getValue();+ const numDigitsValue = Math.trunc(+_numDigitsObject.getValue());+ const result = this._roundBank(numberValue, numDigitsValue);++ return NumberValueObject.create(result);+ });++ if (maxRowLength === 1 && maxColumnLength === 1) {+ return (resultArray as ArrayValueObject).get(0, 0) as BaseValueObject;+ }++ return resultArray;+ }++ private _roundBank(number: number, numDigits: number): number {+ // logic is:+ // If the digit to be rounded is 4 or less than, it is discarded.+ // If the digit to be rounded is 6 or greater, it rounds up.+ // If the digit to be rounded is exactly 5:+ // If there are any digits following the 5, it rounded up.+ // If there are no digits following the 5, If the preceding digit is odd, it rounds up. If the preceding digit is even, it is discarded.+ const EPSILON = 1e-8;+ const multiplier = 10 ** numDigits;+ const adjustedNum = +(number * multiplier).toFixed(8);+ const integerPart = Math.floor(adjustedNum);+ const decimalPart = adjustedNum - integerPart;++ let result = Math.round(adjustedNum);++ if (decimalPart > 0.5 - EPSILON && decimalPart < 0.5 + EPSILON) {+ result = integerPart % 2 === 0 ? integerPart : integerPart + 1;+ }++ return numDigits ? result / multiplier : result;+ }+}diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts b/packages/sheets-formula-ui/src/locale/function-list/math/en-US.tsindex ef425f1562..c5c50ce5cd 100644--- a/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts+++ b/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts@@ -817,6 +817,20 @@ export default {numDigits: { name: 'num_digits', detail: 'The number of digits to which you want to round the number argument.' },},},+ ROUNDBANK: {+ description: 'Rounds a number in banker\'s rounding',+ abstract: 'Rounds a number in banker\'s rounding',+ links: [+ {+ title: 'Instruction',+ url: '',+ },+ ],+ functionParameter: {+ number: { name: 'number', detail: 'The number that you want to round in banker\'s rounding.' },+ numDigits: { name: 'num_digits', detail: 'The number of digits to which you want to round in banker\'s rounding.' },+ },+ },ROUNDDOWN: {description: 'Rounds a number down, toward zero',abstract: 'Rounds a number down, toward zero',diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts b/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.tsindex 5b6883c6b9..643d3ecca4 100644--- a/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts+++ b/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts@@ -817,6 +817,20 @@ export default {numDigits: { name: '桁数', detail: '数値を四捨五入した結果の桁数を指定します。' },},},+ ROUNDBANK: {+ description: '銀行家の丸めで数値を丸めます',+ abstract: '銀行家の丸めで数値を丸めます',+ links: [+ {+ title: '指導',+ url: '',+ },+ ],+ functionParameter: {+ number: { name: '数値', detail: '銀行型丸めで丸める数値。' },+ numDigits: { name: '桁数', detail: '銀行型丸めで丸める桁数。' },+ },+ },ROUNDDOWN: {description: '数値を指定された桁数で切り捨てます。',abstract: '数値を指定された桁数で切り捨てます。',diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts b/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.tsindex 079721cb62..f676724069 100644--- a/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts+++ b/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts@@ -772,6 +772,20 @@ export default {numDigits: { name: 'số chữ số', detail: 'Số chữ số mà bạn muốn làm tròn số tới đó.' },},},+ ROUNDBANK: {+ description: 'Làm tròn một số theo cách làm tròn của ngân hàng.',+ abstract: 'Làm tròn một số theo cách làm tròn của ngân hàng.',+ links: [+ {+ title: 'Hướng dẫn',+ url: '',+ },+ ],+ functionParameter: {+ number: { name: 'số', detail: 'Số mà bạn muốn làm tròn theo cách làm tròn của ngân hàng.' },+ numDigits: { name: 'số chữ số', detail: 'Số chữ số mà bạn muốn làm tròn theo cách làm tròn của ngân hàng.' },+ },+ },ROUNDDOWN: {description: 'Làm tròn số xuống, tiến tới không.',abstract: 'Làm tròn số xuống, tiến tới không.',diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts b/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.tsindex 36cbe47b13..8d593d3e95 100644--- a/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts+++ b/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts@@ -816,6 +816,20 @@ export default {numDigits: { name: '位数', detail: '要进行四舍五入运算的位数。' },},},+ ROUNDBANK: {+ description: '通过“四舍六入五成双”舍入数字',+ abstract: '通过“四舍六入五成双”舍入数字',+ links: [+ {+ title: '教学',+ url: '',+ },+ ],+ functionParameter: {+ number: { name: '数值', detail: '要“四舍六入五成双”的数字。' },+ numDigits: { name: '位数', detail: '要进行“四舍六入五成双”运算的位数。' },+ },+ },ROUNDDOWN: {description: '向绝对值减小的方向舍入数字',abstract: '向绝对值减小的方向舍入数字',diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts b/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.tsindex d2adb48ac2..9ee17a1a91 100644--- a/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts+++ b/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts@@ -815,6 +815,20 @@ export default {numDigits: { name: '位數', detail: '要對數字引數進位的位數。' },},},+ ROUNDBANK: {+ description: '依照「四舍六入五成雙」舍入數字',+ abstract: '依照「四舍六入五成雙」舍入數字',+ links: [+ {+ title: '教導',+ url: '',+ },+ ],+ functionParameter: {+ number: { name: '數值', detail: '要「四舍六入五成雙」的數字。' },+ numDigits: { name: '位數', detail: '要進行「四舍六入五成雙」運算的位數。' },+ },+ },ROUNDDOWN: {description: '向絕對值減小的方向舍入數字',abstract: '向絕對值減小的方向捨去數字',diff --git a/packages/sheets-formula/src/services/function-list/math.ts b/packages/sheets-formula/src/services/function-list/math.tsindex 2220c92e2a..e6d3d09128 100644--- a/packages/sheets-formula/src/services/function-list/math.ts+++ b/packages/sheets-formula/src/services/function-list/math.ts@@ -1127,6 +1127,28 @@ export const FUNCTION_LIST_MATH: IFunctionInfo[] = [},],},+ {+ functionName: FUNCTION_NAMES_MATH.ROUNDBANK,+ functionType: FunctionType.Math,+ description: 'formula.functionList.ROUNDBANK.description',+ abstract: 'formula.functionList.ROUNDBANK.abstract',+ functionParameter: [+ {+ name: 'formula.functionList.ROUNDBANK.functionParameter.number.name',+ detail: 'formula.functionList.ROUNDBANK.functionParameter.number.detail',+ example: '2.345',+ require: 1,+ repeat: 0,+ },+ {+ name: 'formula.functionList.ROUNDBANK.functionParameter.numDigits.name',+ detail: 'formula.functionList.ROUNDBANK.functionParameter.numDigits.detail',+ example: '2',+ require: 1,+ repeat: 0,+ },+ ],+ },{functionName: FUNCTION_NAMES_MATH.ROUNDDOWN,functionType: FunctionType.Math,