FROM CACHE - en_header

Technical Q&A

Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Shopify Community
- :
- Shopify Discussion
- :
- Technical Q&A
- :
- Remove decimals from whole numbers for variable results

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Remove decimals from whole numbers for variable results

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-07-2022
11:52 PM

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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
12:10 AM

yea, it's possible.

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

use the conditional statement. to work

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:02 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:04 AM

@Sneaky

You want to remove the decimal from the variables which you have used.

right?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:05 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:15 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:14 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

03-08-2022
01:18 AM

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

12-12-2023
01:05 AM

You can try that:

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

02-02-2024
09:22 PM

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>
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

09-04-2024
05:05 PM

You can use {{ var | * money_without_trailing_zeros *}}

The 9 Questions You Need To Answer When Starting a B2B Store
**JasonH**
Sep 23, 2024

Starting a B2B store is a big undertaking that requires careful planning and execution. W...

By
Get ready for peak holiday selling season 2024
**Jacqui**
Sep 11, 2024

By investing 30 minutes of your time, you can unlock the potential for increased sales,...

By
Highlights | Top Contributors August '24
**JasonH**
Sep 9, 2024

We appreciate the diverse ways you participate in and engage with the Shopify Communi...

By