We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Here's a Number Format Plugin

Development and Programming

Robert Wallis's avatar
Robert Wallis
36 posts
17 years ago
Robert Wallis's avatar Robert Wallis

Surely someone has already done this and I am just a lazy searcher. But here’s something that often comes up so I made a plugin for myself, and you guys can have it too.

Imagine, you have a shopping cart with products in it. So in the template you say something like:

{exp:my_shopping_cart}
<h2>Cool Item</h2>
Only ${price} !!!11!one!!
{/exp:my_shopping_cart}

But this gets rendered as follows with no commas or decimal points :(

<h2>Cool Item</h2>
Only $12000 !!!11!one!!

So what do you do? Add PHP to that template? Noooo, you use this nifty plugin.

{exp:my_shopping_cart}
<h2>Cool Item</h2>
Only ${exp:number_format number="{price}" decimals="2"} !!!11!one!!
{/exp:my_shopping_cart}

And it renders the way you wanted it:

<h2>Cool Item</h2>
Only $12,000.00 !!!11!one!!

Basically it’s just a wrapper for PHP’s number_format function.

       
PXLated's avatar
PXLated
1,800 posts
17 years ago
PXLated's avatar PXLated

Cool. But I think I would name it something different since it’s not necessarily specific to the shopping cart.

       
Robert Wallis's avatar
Robert Wallis
36 posts
17 years ago
Robert Wallis's avatar Robert Wallis

Hey guys, thanks for the replies. In the usage section of the plugin there are examples on how to change the thousands_seperator or the decimal_point. I didn’t choose those names, PHP did with their awesome open source coding standards, oh, wait, never mind. Please see the PHP doc for more information on how number_format works, all the parameters are named the same and are just passed through to the function. http://php.net/manual/en/function.number-format.php

Also it’s named number_format because that’s the name PHP chose. I used the shopping cart example, because I’m using it in my shopping cart.

       
Sasha's avatar
Sasha
85 posts
17 years ago
Sasha's avatar Sasha

Just wanted to say thanks for this plugin! I was trying to find a way to do this for displaying the total number of images in my gallery - once it hit 10,000 I was starting to sorely miss number_format. So this was perfect - thanks for writing & releasing it! 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.