During the template creation process of AnQi CMS, we often need to flexibly handle data to display the most user-friendly information on the frontend page.Numbers are a common form of data, and sometimes we may need to extract a specific value from a longer number. At this point,get_digitThe filter can be put to good use, it can help us easily obtain the Nth digit from the end of a number.

get_digitThe core function and purpose of the filter

get_digitA filter, as the name implies, is a tool used to 'extract numbers' from numbers. Its uniqueness lies in the fact that it extracts theendStart counting. This means that when you specify 'the Nth digit from the end', it is counted from the rightmost side of the number, for example, the units digit is the first digit from the end, the tens digit is the second digit from the end, the hundreds digit is the third digit from the end, and so on.

This filter accepts an optional parameter, which is the 'N' value you wish to obtain. The N represents the number of digits from right to left.

For example, if there is a number1234567890:

  • The first digit from the end is0
  • The second last is9
  • The third last is8
  • The tenth last is1

How to useget_digitFilter

In Anqin CMS template syntax, filters are usually used through the pipe symbol|Pass the variable to the filter.get_digitThe filter does not apply either. Its basic syntax structure is{{ 数字变量 | get_digit:N }}.

We will demonstrate its usage through several specific examples:

  1. Get the digit at the specified last place:Suppose we have a number1234567890We want to get the third last digit (i.e.8):

    {# 假设我们有一个数字变量:numberValue = 1234567890 #}
    
    
    {# 获取倒数第三位的数字(即 8)#}
    {{ 1234567890|get_digit:3 }}
    
  2. Get the last digit:If you want to get the last digit, you can set N to:1:

    {# 获取倒数第一位的数字(即 0)#}
    {{ 1234567890|get_digit:1 }}
    
  3. When N is 0 or omitted:If the N parameter is omitted, or you set it to0,get_digitThe filter will return the entire original number. This can be used as a default value or a check mechanism in some cases.

    {# N为0时,返回整个数字 #}
    {{ 1234567890|get_digit:0 }}
    
    
    {# N省略时,也返回整个数字 #}
    {{ 1234567890|get_digit }}
    
  4. When N exceeds the total number of digits of the number:If the value of N is greater than the total number of digits, for example, if you ask for the 15th digit from the end of a 10-digit number, the filter will still return the full original number.

    {# N超出数字位数(10位数字获取倒数第15位),返回整个数字 #}
    {{ 1234567890|get_digit:15 }}
    

Points to note

  • Input type: get_digitThe filter is mainly designed to process numeric data or strings containing only numbers (such as"12345")。For non-numeric string types, the behavior may differ from what is expected, and it is recommended to avoid using it for plain text strings to prevent unexpected results.
  • Counting direction:Always rememberget_digitIs from the number of theendStarting from the right, count to the left.

Application scenarios in practice

get_digitFilters may play a role in various website operation scenarios:

  • Data analysis and grouping:When you need to classify or group data based on a specific digit of a number (for example, the specific digits of an order number may represent an area or channel).
  • Formatted display: In certain special report or display requirements, it is necessary to extract a digit of a number for independent display or calculation.
  • Simple verification:Even though it is not a professional verification tool, it can be used to quickly check if the specific digits of a number meet expectations in some simple template logic.

Through proficiencyget_digitFilter, you can control and display digital data more finely in Anqi CMS templates, thereby enhancing the interactivity of website content and the accuracy of information delivery.


Frequently Asked Questions (FAQ)

Q1:get_digitFilters andsliceWhat are the differences between filters?

A1: get_digitThe filter is specifically used to extract fromNumberExtractThe Nth last characterSingle digit, it will process the internal structure of the number. WhilesliceThe filter is a more general tool that can be used to truncatestringorarrayofContinuous sectionCannot directly understand the concept of 'the Nth digit' of a number. If the number is first converted to a string, thensliceThis can be used to extract a character from a string, but it is notget_digitThe direct use of.

Q2: If my number is in text format, for example"12345",get_digitDoes the filter work properly?

A2:It's okay. Just pass inget_digitThe filter is a string containing only numbers (for example"12345"),It usually can be correctly identified and processed, extracting the Nth digit from the end of the number according to the logic of numbers. However, if the string contains non-numeric characters (such as"abc123"Its behavior may become unpredictable or return a default value.

Q3: I need to get the number infrom left to rightthe Nth digit,get_digitCan it be done?

A3: get_digitThe filter counts strictly from right to left (counting backwards), and it does not provide a direct function for counting from left to right. If you need to get a certain digit from the left of the number, the usual practice is to convert the number to a string first, then use the logic of string processing, combined with the length of the string andsliceA filter is used to implement indirectly. But this will be more complex thanget_digitextracting the last digits directly.