Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Formatting decimal and money values?? Possible duplicate

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

Page: 1
Go to End
Author Topic: 502 Views
  • Nadz
    Avatar
    Community Member
    8 Posts

    Formatting decimal and money values?? Possible duplicate Link to this post

    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

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: Formatting decimal and money values?? Possible duplicate Link to this post

    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

    502 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.