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.

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.

Converting Price (with a Decimal) from String to Number

Use Case: You have a field in a data feed you maintain with a price, but price is stored as a string and not a number. You need to do some mathematical operations, so it’s integral that the value is a number. The value has a decimal place. You’ll use map(), int(), strpos(), strrpos(), and substr() to successfully convert this value from a string to a number.

Zephyr:

In the Setup:

{content = map(content, lambda c: c + { "newMembershipPrice": int(substr(c.vars.membership_price,0,strpos(c.vars.membership_price,"."))) + int(substr(c.vars.price,strrpos(c.vars.membership_price,".")+1))/100 })}

In the Code:

{if profile.vars.member == true}
 As a member, here are your savings on each item!
{foreach content as c}
 {if length(c.vars.membership_price) > 0}
 ${number(c.price/100 - c.newMembershipPrice,2)} on {c.title}!
 {/if}
{/foreach}
{/if}

Output:

As a member, here are your savings on each item!

$2.01 on To Kill a Mockingbird!

$1.84 on Salem’s Lot!

$124.25 on Women’s Leather Jacket!

$24.50 on Men’s Chelsea Boots!

Explanation:

This script uses the map() function to add a new parameter to a content object, in this instead a parameter called “newMembershipPrice,” which will be the numerical value of a price point currently being stored as a string. In order to determine the value for this parameter, the substr() function then takes a substring of the “membership_price” custom variable, using the strpos() function to start at the beginning of the string and finding the numerical string position of the decimal point as the endpoint. For instance, using the first item in the eComm feed as an example, “12.99” would become “12”. In order to convert this to a number, the int() function is used on that substring.

Since int() drops everything after a decimal place, the value of the cents is isolated in a similar fashion. Using substr() to isolate the value, strrpos() looks at the the same “membership_price” custom variable, finding the numerical position of the decimal point, and then adding one, isolating the “cents.” Like the first part of this script, int() is then used to convert this value to a number. In order to re-convert this number into to cents, it is the divided by 100, and going off the same example, would result in “.99″. The two numberical substrings are added together, producing “12.99”, now as a number instead of a string.

In order to leverage this new value in the Code, an “if” statement is used to check for a custom field called “member” (this can be anything of your choosing, however). If “member” equals “true”, then display the savings each user will receive as a member. Next, a foreach statement loops through each piece of content, and an “if” statement checks to see if the item has a “membership_price” field associated. If so (i.e., there’s a discounted price), the item’s price is divided by 100 as to convert it to a dollars/cents format and then the newMembershipPrice value is subtracted. The number() function is then used to convert this value to a standardized format, such as “2.01”.

Top