Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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?
yea, it's possible.
https://shopify.github.io/liquid/filters/round/
use the conditional statement. to work
Can you give me an example based on what I have above?
@Sneaky
You want to remove the decimal from the variables which you have used.
right?
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
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
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
You can try that:
{% assign value = 10.0 %}
{% assign valueRound = value | round: 0 %}
{% if value == valueRound %}
{% assign value = valueRound %}
{% endif %}
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>
You can use {{ var | money_without_trailing_zeros }}
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024