Calculate Macro

Here is a simple macro that lets you calculate an expression any time you are typing text. You type an expansion string like:

=calc=5*3+7=

and Keyboard Maestro will type the resulting answer for you. Naturally, all the Keyboard Maestro Functions are available.

Calculate.kmmacros (2.2 KB)

7 Likes

Hi, Peter,

This is brilliant! It will save me trips to Spotlight, my calculator of choice when the HP is too far out of reach.

I made one change to your macro which allows more complex math(s) to occur at the expense of useless trailing zeros showing up when doing non-integer math(s):

(That's a pipe "|" between the double quote and the "bc", and that's a lower-case "L" at the end of the expression.)

Change the "scale=" expression to the number of decimal places you wish when the result is not an integer. I can't seem to find a way to make bc act any more sensibly, but it does work. The "-l" option is entirely optional, but it does get you a few trig and other functions. See the manpage for bc here:

https://www.gnu.org/software/bc/manual/html_mono/bc.html

...and then I realized just how ridiculous I was being to replace the Calculate filter, with its extensive list of functions, with bc, and its meager set of functions. I did, however, learn things along the way, so all is not lost.

But the reason I did it in the first place is because I tried the expression "sqrt(2)" and got... nothin'. (I'm an EE, so sqrt(2) is somewhat important.) So I thought, Heck, if Calculate can't do a square root, it must be rather basic. Better write a replacement. (What little I knew!)

So that leaves me with the question, Did I miss "sqrt" somewhere in the list, or is it truly not one of the Calculate functions?

Thanks again for this delicious macro!

Bill

Yeah, its weird that their is no square root function - except this is a Mac, it has a square root character: Option-V (√).

=calc=√2= ➤ 1.41421356
1 Like

And I learn something else! Who knew that the special characters were actually operators?!

Thanks!

Great macro Peter!

To all readers: If you liked that macro, you may also be interested in:

I just checked and it is documented in the Calculations user manual section, so if you want to see if there is anything else there of interest, that mighty be worth taking a look at.

I have added the following Action to Peter's macro just before the last Action of "Insert Text":

Limit the Number of Decimals In Output

This rounds to two decimal places, IF more than two are the result of the calculation.
IF Calc is already less than two decimals, then it is output as is.

Of course, you could change to any number of places, just by replacing the "100" in the calculation with another power of 10.

1 Like

Thanks for this macro and its addition!

With these settings:

Is it possible to use the comma instead of the full stop as the decimal delimiter?

And: Is it possible to always have two decimals (as is default in currency calculations)?

On a side note: I'm not a programmer, so I was a little puzzled by the notation with the three equal signs. Besides that, I wanted to have the originating calculation inserted too:

Try disabling or deleting the rounding action and replacing the text inserted in the final action with this:

%Variable%Calc% = %CalculateFormat%Result%Currency%

It should format the number in the Result variable according to your localized currency notation. A bit more information on the CalculateFormat token (which is new to KM 8 and thus not part of this original thread) here: https://wiki.keyboardmaestro.com/token/CalculateFormat

2 Likes

In Keyboard Maestro you mean?

No, Keyboard Maestro calculations always use a full stop as a decimal point, there is no localisation for this.

You can use the CalculateFormat token to convert a number into formatted text in a variety of localised ways including currency format.

1 Like

In Keyboard Maestro you mean?

That's what I expected (that the decimal point is used internally for calculations). Well, I can always filter the variable to replace the point with a comma before it is typed.

This is what the CalculateFormat token is for. Assuming you have a variable named Number with a value it in, you can use the text token:

%CalculateFormat%Number%Decimal%

And it will format it using localised conventions.

Same for:

%CalculateFormat%Number%#,##0.00%

Even though the format there shows "," as a separator and "." as a decimal, they will be replaced with the localised equivalents (maybe space and , for example).

2 Likes