Skip to content

Instantly share code, notes, and snippets.

@joewils
Last active March 7, 2022 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewils/b18fd97d5a1a58b9ebc8 to your computer and use it in GitHub Desktop.
Save joewils/b18fd97d5a1a58b9ebc8 to your computer and use it in GitHub Desktop.
USD currency formatting filter for Liquid templates
# Custom text filters
module TextFilter
# USD Currency
def currency(input)
return sprintf('$%0.2f',input).gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end
end
# Globally register custom text filters
Liquid::Template.register_filter(TextFilter)
# Test
@template = Liquid::Template.parse(" {{ 1000.009234 | currency }} ")
puts @template.render
@joewils
Copy link
Author

joewils commented Jul 29, 2015

sprintf magic stolen / borrowed from Mike Pence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment