Trailing zero's in Jekyll/Liquid
Asked Answered
C

4

8

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.

Colatitude answered 16/6, 2016 at 14:30 Comment(0)
G
11

This would be one way to do it, in multiple steps:

  • Round the number N decimal places (e.g. 2) so decimals have a uniform size;
  • Split the number by the decimal separator, so you get an array with two elements, with the integral part on element 0, and the fractional part on element 1;
  • Append N trailing zeros to the fractional part, and truncate the string by N, so you get exactly N decimals.

{% 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 }}
Gotthard answered 17/6, 2016 at 1:11 Comment(4)
It shows what an idiotic templating language we are dealing with, if this is actually needed to solve this.Bailar
@NickN. I understand your frustration - and I don't like the hack above either -, but consider that Liquid is a free and open-source technology that does a lot of things very well, but happen to have a missing feature to deal with formatting numbers. We are free to send a pull-request to implement that ourselves, or pay someone to do it... I don't think calling it idiotic is fair...Gotthard
@CaioProiete well said, I agree with you, shouldn't have called it that. Can't edit my comment though :(Bailar
idiotic may be harsh, and I agree that Liquid does a lot of stuff very well, but the way in which it deals with numbers like this certainly is an oversight - and in my opinion, a fairly substantial one. An excellent solution btw.Grayling
D
6

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 }}
Discerning answered 15/2, 2020 at 20:53 Comment(2)
Great! Love it!Colatitude
So elegant. This should be the accepted answerPotemkin
A
5

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"
Acima answered 16/6, 2016 at 17:20 Comment(1)
Can you show me how to hack it together? As I have not found a substring filter... Therefore I can multiply by 100, but I cannot add a point at the right spot. Any ideas?Colatitude
L
0

Just updating the above answer for the case where you get a number with no leading zeros like 50

              {% assign price_split = price | round: 2 | split: "." %}
              {% assign integral = price_split[0] %}
              {% if price_split[1] %}
              {% assign fractional = price_split[1] | append: "00" | truncate: 2, "" %}
              {% else %}
              {% assign fractional = "00" %}
              {% endif %}
              <b>\${{integral}}.{{fractional}}</b>
Laywoman answered 21/6, 2023 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.