Number and currency filters

Number and currency filters manipulate numbers. Common uses for number and currency filters include rounding numbers, displaying numbers as a variety of currency types, and formatting numbers by adding commas or other delimiters. 

Let’s say you don’t want the cents displayed in the example above. Let’s add a this money_without_trailing_zeros filter to the mix:

Code (key and filters)Pamper your pet and save 25%! This week, our super-plush senior dog bed is just ${{ product.product_price | times: .75 | money_without_trailing_zeros }}.
Output Pamper your pet and save 25%! This week, our super-plush senior dog bed is just $75.

Remember how we said filters are applied in order from left to right? The above is a great example: you want to put times: .75  first to do the mathematical calculation on the value, then add money_without_trailing_zeros to remove the zeros from the result before our modified value is output.  

Practice problem: number and currency filters

Let’s take another approach to displaying discounts with number and currency filters. Here’s the copy we want to display:

You get $20 off! The original price is $59.99, and you’ll only pay ___. 

The key that contains the value is product.product_price. Use the subtract filter to remove $20 from the original price, product.product_price. Format the result as currency using the money filter. (Assume your money filter adds the currency symbol you’ve pre-selected plus two decimals.)

Select the right Liquid code from the options below to fill in the blank:

You get $20 off! The original price is $59.99, and you’ll only pay ___. 

  1. {{ product_price | minus: 20 | money }}
  2. {{ product.product_price | minus: 20 | money }}
  3. {{ product.product_price | money | minus: 20 }}
  4. {{ product.product_price | minus: 20 | money: 2 }}

The correct answer is B or C (in this case, the order of operations doesn’t matter)!

Let’s run the code: 

Code (key and filter)You get $20 off! The original price is {{ product.product_price | money }}, and you’ll only pay {{ product.product_price | minus: 20 | money }}. 
Output You get $20 off! The original price is $59.99, and you’ll only pay $39.99.

Why don’t the other options work?

a. You must reference the entire key: product.product_price

d. The money filter, as we’ve defined it for this problem, will always and only display two decimal points. Adding the extra colon, space, and integer may break your code.