Forum Discussion

EiC4733's avatar
EiC4733
Brass Contributor
Jan 16, 2023
Solved

How to format a list column to have the numbers in the format "0000"

Everything is in the question.


I would like to format a number title column of one of my list to have four digits.

For example "45" will be "0045".

 

Could you help me?

  • EiC4733 you can format the column with the following JSON to achieve this:

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=if(@currentField <= 9, '000'+'@currentField', (if(@currentField >= 10 && @currentField <= 99,'00'+'@currentField', (if(@currentField >= 1000 ,'@currentField','0'+'@currentField'))"
    }

     

    Rob
    Los Gallardos
    Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

     

4 Replies

  • EiC4733 You can also use the padStart function to simplify.

     

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=padStart(toString(@currentField),4,'0')"
    }

     

    Good Luck!

    Don


    Please click Mark as Best Response & Like if my post helped you to answer or resolve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

  • RobElliott's avatar
    RobElliott
    Silver Contributor

    EiC4733 you can format the column with the following JSON to achieve this:

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
      "elmType": "div",
      "txtContent": "=if(@currentField <= 9, '000'+'@currentField', (if(@currentField >= 10 && @currentField <= 99,'00'+'@currentField', (if(@currentField >= 1000 ,'@currentField','0'+'@currentField'))"
    }

     

    Rob
    Los Gallardos
    Intranet, SharePoint and Power Platform Manager (and classic 1967 Morris Traveller driver)

     

    • EiC4733's avatar
      EiC4733
      Brass Contributor

      RobElliott 

       

      the formatting code works perfectly but after several months of use I realize that this does not solve my problem. Users can always enter more than 4 numbers or even letters whereas the cell should only allow 4 number type characters

Resources