Skip to content

Commit

Permalink
fix(formula): fix decimal bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Jul 19, 2024
1 parent fcc7e6b commit d78d2bc
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/engine-formula/src/functions/math/decimal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class Decimal extends BaseFunction {
return NumberValueObject.create(0);
}

if (!this.isValidCharForRadix(textValue, radixValue)) {
return ErrorValueObject.create(ErrorType.NUM);
}

const result = Number.parseInt(textValue, radixValue);

if (Number.isNaN(result)) {
Expand All @@ -107,4 +111,20 @@ export class Decimal extends BaseFunction {

return resultArray;
}

private isValidCharForRadix(text: string, radix: number): boolean {
for (const char of text) {
const charCode = char.toUpperCase().charCodeAt(0);

if (radix <= 10 && !(charCode >= 48 && charCode < 48 + radix)) { // '0' to 'radix-1'
return false;
}

if (radix > 10 && !((charCode >= 48 && charCode < 58) || (charCode >= 65 && charCode < 65 + radix - 10))) { // '0' to '9' 'A' to 'A+radix-10'
return false;
}
}

return true;
}
}

0 comments on commit d78d2bc

Please sign in to comment.