I have frontmatter that looks like this:
products:
- item: item name
price: 39.50
- item: item number two
price: 12.50
How can I output these variables in liquid with trailing zero's?
Note that {{ products[0].price }}
will output 39.5
. I need it to output 39.50
.
This would be one way to do it, in multiple steps:
{% assign price_split = page.products[0].price | round: 2 | split: "." %}
{% assign integral = price_split[0] %}
{% assign fractional = price_split[1] | append: "00" | truncate: 2, "" %}
Formatted Price: {{ integral }}.{{ fractional }}
I'm sure you can hack it together using a series of filters, but a quick and dumb solution would be to simply wrap your price
in quotation marks to make it a string value instead of a number value. In that case, it will come out exactly as you typed it, including any trailing zeroes.
products:
- item: item name
price: "39.50"
- item: item number two
price: "12.50"
If you need to do stuff with the numeric value, you could have two variables: a number value for the price and a string value for the price label.
products:
- item: item name
price: 39.50
priceLabel: "39.50"
- item: item number two
price: 12.50
priceLabel: "12.50"
I know this is an older question but you can create a custom filter for this.
module Jekyll
module PrecisionFilter
def precision(input, value=0)
("%.#{value}f" % input)
end
end
end
Liquid::Template.register_filter(Jekyll::PrecisionFilter)
Save this as _plugins/precision_filter.rb
and restart your jekyll server (if its running). It will give you the ability to set the precision on a number like so,
{{ price | precision: 2 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With