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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Formatting decimal and money values?? Possible duplicate


Go to End


2 Posts   1985 Views

Avatar
Nadz

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.

Nadz

Avatar
zenmonkey

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