About adding Number separator

Hi. Is it possible to add number separator by draft action?
Like I have a number like 574839203, can I have an action separate the number like this, 574 839 203? I think it’s difficult to do this, but still want to ask here.

Once you know the length of the string of numbers, you can in effect calculate where to insert the spaces.

There are many ways to tackle this, but I think this one will be relatively easy to follow and straight forward to write a script for.

If you insert every third character you could prefix with spaces to make the length up to a multiple of 3. Then slice off every three characters and add to a new string each time followed by a space. Repeat until no digits are left to process. You now have the spaces in place, but potentially extra spaces at the start and certainly one at the end. You can then “trim” the string to remove spaces at the start and end.

Javascript has number formatting functions built-in. Here’s an example action which applies number formatting to the selected text (which must be a number to work).

The code for this is pretty straight-forward:

let sel = editor.getSelectedText();
let n = parseFloat(sel);

if (!n) {
	console.log("Text selection must be number");
	context.cancel();
}
else {
	editor.setSelectedText(n.toLocaleString("en-US"));
}