What's your biggest current challenge? Have your say in Community Polls along the right column.

Metafield Decimal remove the .0 if there isn't a value

Solved

Metafield Decimal remove the .0 if there isn't a value

4cexntr
New Member
5 0 0

I set up several decimal Metafields in my store to store to display dimensions but the values are defaulting to display .0 at the end of the number. I only want it to show the decimal if there is actually a decimal value. Anyone know how to do this?

 

Here is an example:

Metafield Value = 12.0

The way I want it to display is = 12 

 

Metafield Value = 12.05

The way I want it to display is = 12.05 

 

Accepted Solution (1)
Uhrick
Shopify Partner
433 68 102

This is an accepted solution.

and I don't know if I understood correctly, but to join our codes, it would be

{% if product.metafields.my_fields.arm_h.value != blank %}
{% assign metafield_ceil = product.metafields.my_fields.arm_h.value | ceil %}
arm h. {% if metafield_ceil == product.metafields.my_fields.arm_h.value %} {{ product.metafields.my_fields.arm_h.value | round }} {% else %} {{ product.metafields.my_fields.arm_h.value }} {% endif %}
{% endif %}
Shopify Developer
Hire me for theme customizations at ricardomsilvestre@outlook.com or Upwork
Was my answer helpful to you? Please like or mark as solution

View solution in original post

Replies 7 (7)

Uhrick
Shopify Partner
433 68 102

hey, 4cexntr

try this where you are calling your code, replacing metafield_value with the variable that calls  your metafield value:

{% assign metafield_ceil = metafield_value | ceil %}
{% if metafield_ceil == metafield_value %}
{{ metafield_value | round }}
{% else %}
{{ metafield_value }}
{% endif %}

 

Shopify Developer
Hire me for theme customizations at ricardomsilvestre@outlook.com or Upwork
Was my answer helpful to you? Please like or mark as solution
4cexntr
New Member
5 0 0

Wow Uhrick! Thank you for responding and this worked but I'm hoping you might be able to help me with a combo of the code you wrote, plus what I already have.

 

The code I have now displays some text and the meta-value, but it's blank it wont show anything. Is there a way to combine the two so that it doesnt show the .0 and also doesnt show the value / text if blank?

 

Here is my current code:

{% if product.metafields.my_fields.arm_h.value != blank %}arm h. {{ product.metafields.my_fields.arm_h.value }}"
{% endif %}

 

 

Uhrick
Shopify Partner
433 68 102

is this comma after }} closing anything? And maybe try separating arm h. from %}, your code seems to be functional in general. 

Other than that, if my answer helped, I'd appreciate if you could like it or mark as solution

Shopify Developer
Hire me for theme customizations at ricardomsilvestre@outlook.com or Upwork
Was my answer helpful to you? Please like or mark as solution
Uhrick
Shopify Partner
433 68 102

This is an accepted solution.

and I don't know if I understood correctly, but to join our codes, it would be

{% if product.metafields.my_fields.arm_h.value != blank %}
{% assign metafield_ceil = product.metafields.my_fields.arm_h.value | ceil %}
arm h. {% if metafield_ceil == product.metafields.my_fields.arm_h.value %} {{ product.metafields.my_fields.arm_h.value | round }} {% else %} {{ product.metafields.my_fields.arm_h.value }} {% endif %}
{% endif %}
Shopify Developer
Hire me for theme customizations at ricardomsilvestre@outlook.com or Upwork
Was my answer helpful to you? Please like or mark as solution
4cexntr
New Member
5 0 0

Worked perfectly. Thank you for your help!!!

nelsonandco
Visitor
1 0 0

Thank you for this solution. Can you tell me what file this code needs to go in?

Uhrick
Shopify Partner
433 68 102

hey, nelson. It should go in whatever file you need to get the metafields in. If you want them in the product page, there is probably a file called 'main-product.liquid' in the Section folder of your theme code

Shopify Developer
Hire me for theme customizations at ricardomsilvestre@outlook.com or Upwork
Was my answer helpful to you? Please like or mark as solution