Sneaky
March 8, 2022, 4:52am
1
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?
2 Likes
@Sneaky
yea, it’s possible.
https://shopify.github.io/liquid/filters/round/
use the conditional statement. to work
Sneaky
March 8, 2022, 6:02am
3
Can you give me an example based on what I have above?
2 Likes
@Sneaky
You want to remove the decimal from the variables which you have used.
right?
@Sneaky
{{ product.price | money_without_currency | remove: '.00' }}
try like this.
Sneaky
March 8, 2022, 6:14am
6
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
2 Likes
Sneaky
March 8, 2022, 6:15am
7
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
2 Likes
@Sneaky
you mean only removing the 0 from the decimal point.
Sneaky
March 8, 2022, 6:18am
9
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
1 Like
ThangCQ
December 12, 2023, 6:05am
10
You can try that:
{% assign value = 10.0 %}
{% assign valueRound = value | round: 0 %}
{% if value == valueRound %}
{% assign value = valueRound %}
{% endif %}
1 Like
You can use {{ var | money_without_trailing_zeros }}