Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Formatting decimal and money values?? Possible duplicate

Go to End

2 Posts   1446 Views


Community Member, 8 Posts

10 April 2012 at 5:24pm

I am kinda new to SS.

In my model I have Price as Decimal. In the template views, i can display the Price which is OK.

But I intend to format the Prices as below
20.00 should return 20
25.99 should return 25.99

What is the best approach of getting this done in SS??

Thanks in advance.



Community Member, 545 Posts

11 April 2012 at 6:10am

Well php has a money_format() function which is kind of fun. But at the most basic level you can just do a str_replace() on the price like

function NicePrice() {
  $offset = strpos($this->Price, '.00'); //checks to see if .00 exists in Prices
  if ($offset === false) {
    return str_replace('.00', '', $this->Price); //if .00 exists it replaces it with nothing
  } else {
    return $this->Price; //if it doesn't exist it just returns the original value

Then all you need to do is call $NicePrice in your template