Output formatting

Calculated Fields for ACF supports formatting the formula output using a format string. The format string is passed to the PHP sprintf() function. So any format string that works with that function also works with this plugin. The format string is entered into the “Number format” in the field definition:

Formatting rules #

The PHP formatting string consists of five different segments (well, actually six but in the case of this plugin, only five of them matters), percent sign, flags, width, precision and type specifier put together like this:

%[flags][width][.precision]specifier

Percent sign %

The percent sign marks the beginning of a number format string, it always needs to be there to mark the beginning of a rule to format a number.

Flags (optional)

The flags are used to pad the resulting output if needed. It can tell you how to pad (left or right justify) and what character to use, usually zeros or spaces. The flags often only makes sense if you also use the width described below.

Width

The minimum width of the resulting outputted string. You can use this to force the output to always have the same string length.

Precision

The number of decimals to use when the type is a floating point number. You can use this to force a fixed number of decimals in the output.

Specifier

The specifier defies how to treat the number. Typically the most used values would be “d” for a normal integer and “F” for a floating point number.

Read the full details about the different parts and how to combine them in the PHP documentation.

Examples #

Sometimes the easiest way to explain something is to use examples. Here are a few common format string that you may find useful:

Integer examples #

Format stringInputOutputExplaination
%d1212Convert the input to an integer.
%d12.3312Like above.
%5d12   12Like above.. Pad the result with spaces so that the total output is 5 characters wide
%05d1200012Like above, but pad using zeros

Floating point examples #

Format stringInputOutputExplaination
%F12.12312.123000Convert the input to a floating point number with 6 decimal places .
%F12.12345678912.123457Like above.
%0.2F12.1234512.12Like above, but with two decimal places
%0.2F1212.00Like above, not that there is always two decimal places.

Currency examples #

Format stringInputOutputExplaination
$%0.2F12$12.00Note that anything before the percent sign will be outputted “as is”. This can be used to add a currency symbol.
%0.2F SEK1212.00 SEKLike above, but with additional characters after the type specification. These are also outputted as is.

Blank if zero #

It’s also possible to omit output if the formula returns zero (0). To do this, toggle the “Blank if zero” field to Yes.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *