How Do I Dynamically Format a Calculation?

I need to dynamically determine the format of a calculation.
This does NOT work:

It treats my %Variable%MyFormat% as if I had entered 0

Any ideas on how to make this work?
Is this a bug, or just an unsupported feature?

Here's my test macro:

###MACRO:   Dynamic Format of Calculation [TEST]

~~~ VER: 1.0    2016-11-25 ~~~

####DOWNLOAD:
Dynamic Format of Calculation.kmmacros (2.9 KB)


Example Results

OK, here is one workaround. Not perfect, but better than nothing.

If Calc was already an integer, it stays an integer when output.
But if Calc has more than 2 decimals, it is rounded to 2 decimals.

This is an unsupported feature. That field is one of the few text fields that does not accept text tokens. I can’t see any particularly good reason for not accepting them (it might cause some backward compatibility, but not much).

Your second solution will result in an inconsistent format - at most 2 decimals, but perhaps less or none. (eg 1, 1.2 or 1.23 are all possible results). Maybe that is what you want, but certainly it is not the same as having a variable containing the format.

You can get the result you want by getting the XML for the action, inserting the value of the MyFormat variable in the XML (carefully depending one its potential values) and then executing the XML of the action via AppleScript. Ugly, but that would accomplish the task.

Alternatively, if there are only a couple possible values of MyFormat, than a switch statement with fixed formats would work and be much easier.

Try this:

Dynamic Calculation with Formatting.kmmacros (5.6 KB)

2 Likes