Formatting Strings

This section of the FreeMarker template is responsible for processing and formatting item dimensions (length, width, height) to three decimal places.

The input string: 5″ x 4″ x 2″

  1. Assign Original String: The dimensions of the item are stored in the dimensions variable.

<#assign dimensions = itemLineDetails.dimensions />

  1. Remove Double Quotes: To facilitate easier processing, double quotes are removed from the dimensions string using the replace function.
<#assign dimensions_cleaned = dimensions?replace(""", "") />
  1. Split Dimensions: The cleaned dimensions string is split into individual parts using the ' x ' delimiter.
<#assign dimension_parts = dimensions_cleaned?split(" x ") />
  1. Initialize Formatted String: A variable formatted_dimensions is initialized to store the formatted result.
<#assign formatted_dimensions = "" />
  1. Loop Through Parts: The code loops through each dimension part, converting it to a number and formatting it to three decimal places.
<#list dimension_parts as part>
    <#assign formatted_part = (part?number)?string("0.000") />
  1. Concatenate Formatted Parts: The formatted parts are concatenated with the appropriate units (" x " or "), depending on whether it’s the last part or not.

<#if part_has_next>

    <#assign formatted_dimensions += formatted_part + "" x " />
<#else>
    <#assign formatted_dimensions += formatted_part + """ />
</#if>
</#list>
  1. Output the Result: Finally, the formatted dimensions are output.

${formatted_dimensions}

Leave a comment

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