Adding leading zero to DataItemIndex using FormatString

Chidozie Bright asked on February 1, 2021 21:14

I am trying to use my DataItemIndex to display numbers with a leading zero in a transformation as follows (01, 02, 03...)

Kentico returns nothing when doing it like this:

{%String.FormatString("{0:D2}", DataItemIndex + 1)%}

If I remove the +1 from the string like below it returns (00, 01, 02) as expected:

{%String.FormatString("{0:D2}", DataItemIndex)%}

Any suggestions on how I get Kentico to allow me to have this leading zero format while incrementing the DatItemIndex by 1 so that my numbering system starts at 01? I've using a variable as well but get the same result. I've also tried wrapping the DataItemIndex + 1 in () but that did work either. I have also tried using DisplayIndex with the same results.

KX12 Portal Engine

Correct Answer

Chidozie Bright answered on February 15, 2021 23:26

I added an additional IF statement in front of my {%String.FormatString(DataItemIndex+1)#%} that adds a 0 if the DataItemIndex was less than 10. Thanks!

0 votesVote for this answer Unmark Correct answer

Recent Answers


Sultan Ahmed answered on February 2, 2021 15:21 (last edited on February 2, 2021 15:21)

The String.FormatString syntax is

String.FormatString(Object format, String parameters)

For that reason you are not able to increment DataItemIndex in the macro. What you can try is

{%String.FormatString("{0:D2}", DataItemIndex).Replace("00","")#%}

0 votesVote for this answer Mark as a Correct answer

Chidozie Bright answered on February 3, 2021 01:31 (last edited on February 3, 2021 01:33)

Hi Sultan,

Your recommendation would just provide a blank result for the first visual set of numbers (00) so it would come out blank, 01, 02... What I am aiming for is to have the transformation run through my index items (page types) starting at the 0-based index and then I want to use that index to also display, visually, the number as a 1-based index with a leading zero. So, for instance DataItemIndex 0 would show as 01, DataItemIndex 1 would show as 02 and so forth.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.