Remove decimals from whole numbers for variable results

Remove decimals from whole numbers for variable results

Sneaky
Shopify Partner
32 3 20

I am using the Dawn theme and making some changes to expand Theme setting options.  What I want to know is if there is a way for a variable to check if it is a whole number and if so remove the decimal point.

So for example, if I have a variable setting thus;

 

(times: 1.0 is to show decimals where needed)

 

let's assume first that settings.my_variable is 10

 

--my-variable: {{settings.my_variable | divided_by: 10.0}};

 

results in 1.0, but since it is a whole number I want it to display as just 1, removing the decimal

 

However, for non-whole numbers, I want to retain the decimals.

So if settings.my_variable is 21 (which will change based on other calculated variables)

 

--my-variable: {{settings.my_variable | divided by: 10 | times: 1.0}};

 

results in 2.1, because it is not a whole number and therefore has a decimal value I want it to display as 2.1.

So if a whole number remove the decimal, otherwise keep it as a decimal number.

Possible or not?

Replies 11 (11)

Zworthkey
Shopify Partner
5581 642 1577

@Sneaky 

yea, it's possible.

https://shopify.github.io/liquid/filters/round/

 

use the conditional statement. to work

Sneaky
Shopify Partner
32 3 20

Can you give me an example based on what I have above?

Zworthkey
Shopify Partner
5581 642 1577

@Sneaky 
You want to remove the decimal from the variables which you have used.
right?

Zworthkey
Shopify Partner
5581 642 1577

@Sneaky 

{{ product.price | money_without_currency | remove: '.00' }}

try like this.

Sneaky
Shopify Partner
32 3 20

Not what is needed, and not price related. 

If the result value is a whole number, like 5.0, then I want it to drop the .0 so displays as 5.  If it is not a hole number and therefore has a decimal value like 6.35, I need it to display with the decimal value, being 6.35

Sneaky
Shopify Partner
32 3 20

I only want the decimal removed IF it is a whole number, so if 1.0, show as 1, if 2.1 show as 2.1

Zworthkey
Shopify Partner
5581 642 1577

@Sneaky 
you mean only removing the 0 from the decimal point.

Sneaky
Shopify Partner
32 3 20

Not quite.  to be clearer;

if the resulting value is;

 

1.0, display as 1 (removing the .0)

2.35, display as 2.35, so retain the full decimal value

6.05, display as 6.05, so retain the full decimal value

9.354 display as 9.354, so retain the full decimal value, regardless of how many decimals

12.00006 display as 12.00006, so retain the full decimal value, regardless of how many decimals.

 

So it is not rounding either

ThangCQ
Shopify Partner
31 3 7

You can try that:

{% assign value = 10.0 %}
{% assign valueRound = value | round: 0 %}
{% if value == valueRound %}
  {% assign value = valueRound %}
{% endif %}

MuhammadAqib
Shopify Partner
14 1 1

Hey,
1. In your Shopify Admin go to online store > themes > actions > edit code and search theme.liquid file.
2. In your theme.liquid file, find the </body> (press CTRL + F  on Windows or command + F on Mac)
3. paste this code right above the </body> tag:

 

<script>
function updatePrice(){
var prices = document.querySelectorAll(`.price-item`);


if (!prices)return;

for (var each of prices){
each.innerText = each.innerText.replace('.00', '');
}
}

 window.onload = function() {
setTimeout(updatePrice, 50);
            }

</script>

Hernan21
Shopify Partner
2 0 0

You can use {{ var | money_without_trailing_zeros }}