Formatting Weight Range : Adjusting decimal places in strings

This section focuses on formatting the weight range (e.g., “10 – 20 lbs”) into a more readable format with one decimal place.

  1. Assign Original String: The weight range string is stored in the dimension_range variable.
<#assign dimension_range = itemLineDetails.weightrange />
  1. Extract Numeric and Unit Parts: The numeric part of the weight range is separated from the unit (lbs), allowing for easier processing.
<#assign numeric_part = dimension_range?replace(" lbs", "") />
<#assign unit = "lbs" />
  1. Split Numeric Part: The numeric part is split into individual values using the ' - ' delimiter.
<#assign range_parts = numeric_part?split(" - ") />
  1. Initialize Formatted String: A variable formatted_range is initialized to store the formatted result.
<#assign formatted_range = "" />
  1. Loop Through Parts: The code loops through each range part, converting it to a number and formatting it to one decimal place.
<#list range_parts as part>
    <#assign formatted_part = (part?number)?string("0.0") />
  1. Concatenate Formatted Parts: The formatted parts are concatenated with the appropriate delimiter (" - "), and finally, the unit (lbs) is added.
<#if part_has_next>
    <#assign formatted_range += formatted_part + " - " />
<#else>
    <#assign formatted_range += formatted_part />
</#if>
</#list>
<#assign formatted_range += " " + unit />
  1. Output the Result: The formatted weight range is then output.
${formatted_range}

Leave a comment

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