How to turn 15000000 to 15,000,000

Hello there,

I'm having trouble figuring out how to accomplish something that seems like it should be simple. I want to change 15000000 to 15,000,000 for me. Additionally, I'd like it to check how many digits are in the number, and if there are nine digits, like in the case of 123000000, I would like it to change the number to 123,000,000. Any assistance with this would be greatly appreciated.

Thank you.

I am not at the computer to try, but maybe you could use the format option.

1 Like

@JimmyHartington has the right idea. But unlike him, I am at my computer, :stuck_out_tongue_winking_eye: so I made a little example to show you how formatting works. You basically just hashtags in place of integers, and whatever formatting you wish, such as comma separators and decimal points. You can even use more hashtags than necessary, as KM will automatically format it according to how many digits the integer has.

Format integers.kmmacros (7.0 KB)

Macro Screenshot (click to expand/collapse)

1 Like

Thank you to both of you. All I needed was to set a variable to itself and change the format according to your instructions. It would be great if Keyboard Maestro could allow user input for defining variables. For example, if I enter 49,000,000 for the first value and 23,000,000 for 2nd value in the user input field, I cannot add two values together.

Keyboard Maestro uses commas as array separators, so a variable containing “49,000,000” is treated as an array of three numbers.

However you can use underscores in numbers, so you can write it like this: “49_000_000”.

Hi Peter, I appreciate your response; however, I’m afraid that won’t work for me as I’ve always separated money with commas. For now, I’ll set a variable to itself and change the format using pound signs. :pray:

Then read the variable as text from the Prompt, and then use the Search and Replace action to remove the , (or replace them with _).

Thanks! Currently, I am utilizing cdthomer’s solution, and it is highly effective.

Yes, that will add the commas for you. My suggestion was to help with the reverse, removing the commas so Keyboard Maestro can process it as a number.

For example, that macro, if you fed it a number containing commas, would fail to do what you want.

That’s correct, and I am sorry for forgetting to mention that I am already doing it.

Screenshot 2023-09-17 at 6.15.16 PM

Then, Once I get the desired input from the keyboard, I use a set variable to add commas in between.

1 Like

Could you elaborate a little on the last part of the script, what’s the point of manually deleting debug variables in this way?

Edit: btw, I know the point is to delete them :grinning:, but I’m wondering aren’t the variables going to go out of scope and be removed automatically when the macro is done running?

Sure! Technically, there’s no such thing as a “debug” variable... there are global, local and instance variables (See the Variables Wiki entry for more info). But global variables are the only kind that persist after a macro has done executing. This is valuable when building, modifying or debugging a macro because it allows you to see the variable’s value even after the macro is done executing, unlike the other two kinds.

However, most of the time, I don’t need to see these variables once the macro is completely built and reliable. Since I, 1) don’t want to see a massive list of variables in my KM preferences window, and 2) global variables seem to slow down macro executions compared to local and instance variables, I like to convert all of my global variables to local or instance once I’ve finished building, modifying or debugging the macro.

But until that time, I sometimes use that AppleScript to delete those debug variables at the end of the macro, particularly when I’m sharing macros on the forum, because I don’t want to clutter up other people’s systems with these global variables. Prepending debug__ to them just makes it easier to make sure I get them all deleted and don’t miss any.

Just saw this part of your comment... no, debug__ variables aren’t deleted automatically, because they’re actually global variables (see the first paragraph of my initial comment).

The whole debug__ part is just something I use to prepend a global variable for easy identification... it’s nothing intrinsic to KM itself. I also use other formats to prepend variables for other purposes... for instance:

  • for generic variables I want to save permanently, I use DND__ (as do many others here)

For other variables I want to save permanently, but are used for specific cases, I’ll use things like:

  • LB__ (for my job)
  • Music__ (for music related things)
  • Finder__ (for macros that work in Finder only) etc.
1 Like