number
Description
Formats a number for display.
string number( float number [, mixed precision ] )
Format a number by rounding and adding comma separators. By default, number()
will round off to the nearest integer value. If you pass a number in the second parameter, it will round to that number of decimal places.
For more precise control over number formatting, you may pass a string in the second parameter instead. This string will be passed to Java’s DecimalFormat class.
number()
can also be used to convert numbers into strings.
Example
{number(37521.5299,2)} = 37,521.53
Create a Reader-Friendly Price
Use Case: You’re passing a “price” parameter on your comment, which Sailthru requires to be in cents. In order to make it reader-friendly, you need to use “number” to convert it to show dollars and cents.Zephyr:
Picked for You:
{foreach content as c}
{c.title} for ${number(c.price/100,2)}!<br/>
{/foreach}
Output: Picked for You: To Kill a Mockingbird for $15.00! Salem’s Lot for $9.99! Women’s Leather Jacket for $450.00! Show/hide sample e-commerce feed used for this example
{ "feed": { "name": "Ecommerce Feed", "url": "http://feed.sailthru.com/ws/feed?id=ecomm" }, "content": [ { "title": "To Kill a Mockingbird", "description":"The unforgettable novel of a childhood in a sleepy Southern town and the crisis of conscience that rocked it.", "date": 1489437759, "image": "http://example.com/fiction/tokillamockingbird-full.jpg", "weight": 75, "tags": [ "site-store", "genre-mystery-thriller", "author-harper-lee", "classics", "fiction", "price-11-20" ], "author": "Harper Lee", "price": 1500, "inventory": 200, "vars": { "sailthru_genre":"mysteries-and-thrillers", "sailthru_category":"books", "membership_price":"12.99" }, "url": "http://example.com/fiction/tokillamockingbird/?utm_medium=site", "images": { "full": { "url": "http://example.com/fiction/tokillamockingbird-full.jpg" }, "thumb": { "url": "http://example.com/fiction/tokillamockingbird-thumb.jpg" } } }, { "title": "Salem's Lot", "description":"Ben Mears has returned to Jerusalem's Lot -- but things are more sinister than he remembers.", "date": 1474547794, "image": "http://example.com/books/fiction/salemslot-full.jpg", "weight": 30, "tags": [ "site-store", "genre-horror", "author-stephen-king", "vampires", "fiction", "price-5-10" ], "author": "Stephen King", "price": 999, "inventory": 1567, "vars": { "sailthru_genre":"horror-and-suspense", "sailthru_category":"books", "membership_price":"8.15" }, "url": "http://example.com/fiction/salemslot/", "images": { "full": { "url": "http://example.com/books/fiction/salemslot-full.jpg" }, "thumb": { "url": "http://example.com/books//fiction/salemslot-thumb.jpg" } } }, { "title": "Women's Leather Jacket", "description":"A classic look that never goes out of style with an exposed metal closure that zips at the cuff.", "date": 1474547794, "image": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg", "weight": 55, "tags": [ "site-store", "womens-clothing", "outerwear", "material-leather", "color-brown", "price-201-300" ], "price": 45000, "inventory": 1720, "vars": { "sailthru_color": "brown", "sailthru_material":"leather", "membership_price":"325.75" }, "url": "http://example.com/fashion/womens/outerwear/leatherjacket/", "images": { "full": { "url": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg" }, "thumb": { "url": "http://example.com/fashion/womens/outerwear/leatherjacket-thumb.jpg" } } }, { "title": "Invisible Monsters: A Novel", "description":"Love, betrayal, petty larceny, and high fashion fuel this deliciously comic novel from the author of <i>Fight Club</i>.", "date": 1474547794, "image": "http://example.com/books/fiction/invisiblemonsters-full.jpg", "weight": 30, "tags": [ "site-store", "genre-mystery-thriller", "author-chuck-palahniuk", "post-modern", "fiction", "price-5-10" ], "author": "Chuck Palahniuk", "price": 899, "inventory": 678, "vars": { "sailthru_genre":"mysteries-and-thrillers", "sailthru_category":"books" }, "url": "http://example.com/fiction/invisiblemonsters/", "images": { "full": { "url": "http://example.com/books/fiction/invisiblemonsters-full.jpg" }, "thumb": { "url": "http://example.com/books//fiction/invisiblemonsters-thumb.jpg" } } }, { "title": "Men's Chelsea Boots", "description": "Look great with jeans or a suit with these slick, black boots.", "date": 1474547794, "image": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg", "weight": 40, "tags": [ "site-store", "mens-clothing", "shoes", "material-suede", "color-black", "price-101-150" ], "price": 14000, "inventory": 15, "vars": { "sailthru_color": "suede", "sailthru_material":"leather", "membership_price":"115.50" }, "url": "http://example.com/fashion/mens/shoes/chelsea-boots/", "images": { "full": { "url": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg" }, "thumb": { "url": "http://example.com/fashion/mens/shoes/chelsea-boots-thumb.jpg" } } }, { "title": "From the Fashion Blog: The Most Comfortable Socks You'll Ever Own", "description":"They'll literally change your life. Find out where and how to get them!", "date": 1489092159, "expire_date": 1520628159, "image": "http://example.com/fashion/most-comfortable-socks-full.jpg", "weight": 25, "tags": [ "site-store", "vertical-fashion", "type-footwear", "author-richard-wilkins", "socks", "unisex-clothing" ], "author": "Richard Wilkins", "vars": { "sailthru_category": "fashion", "sailthru_storyid": 87422 }, "url": "http://example.com/fashion/most-comfortable-socks/", "images": { "full": { "url": "http://example.com/fashion/most-comfortable-socks-full.jpg" }, "thumb": { "url": "http://example.com/fashion/most-comfortable-socks-thumb.jpg" } } } ] }>
Explanation: This script uses a foreach loop to iterate through each item in a data feed, dividing the value of the “price” parameter by “100” (in Sailthru, “price” must be passed in cents). The script then uses tthe number() function to take this value and format it with comma separators to a certain decimal place, in this case, two decimal places.
Pull in Total Lifetime Purchase Amount
Use Case: You want to have a blurb in your email displaying a user’s total purchase amount thanking them for being a loyal customer if they’ve spent over a certain price point.
Zephyr: In the Setup:
{total = 0}
{foreach profile.purchases as i,p} {total = total + p.price} {/foreach}
In the Code:
{if total > 20000} Thanks for being a loyal customer! You've spent ${number(total/100,2)} with us! {/if}
Output: Thanks for being a loyal customer! You’ve spent $250.42 with us!
Explanation: This script uses a lambda, which creates an anonymous function, in conjunction with the length() function to check the “image” field of each item in a content array to see if the length is greater than 1, or in other words, if there’s a value. If there isn’t, the filter() function will remove it from the “content” object. This is beneficial if there’s a chance that you might spider an empty string for the sailthru.image parameter.