Skip to content

Commit

Permalink
Merge pull request #2952 from CKY-/v5-commafyPadUpdate
Browse files Browse the repository at this point in the history
feat: add padding to commafy variable
  • Loading branch information
zunderscore authored Jan 1, 2025
2 parents cf1b9d1 + cba4bc2 commit f799017
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/backend/variables/builtin/number/number-commafy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@ const model : ReplaceVariable = {
{
usage: "commafy[1000000]",
description: `Returns "1,000,000"`
},
{
usage: "commafy[1000000, 2]",
description: `Returns "1,000,000.00"`
}
],
categories: [VariableCategory.NUMBERS],
possibleDataOutput: [OutputDataType.TEXT]
},
evaluator: (
trigger: Trigger,
subject: unknown
subject: number | string,
places: null | number | string
) : string => {
const number = Number(subject);
const numPlaces = Number(places);
if (!Number.isFinite(number)) {
return "[Error: not a number]";
}

if (!Number.isNaN(numPlaces)) {
const number = Number(subject).toPrecision(numPlaces);
const [integer, fraction] = `${number}.0`.split(/\./g);
return `${integer.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}.${fraction}`;
}

return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
};
Expand Down

0 comments on commit f799017

Please sign in to comment.