1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Floats in templates, limit length of string

Discussion in 'XenForo Development Discussions' started by tenants, May 18, 2013.

  1. tenants

    tenants Well-Known Member

    I'm sure I've done this before (but searching though my tempates I can't find the example now)

    I return a float to the template, for instance:

    In the database, myFloat is 4 decimal places, eg: 0.0595
    However, it is displayed as: 0.059500001370907

    In the template, I'm sure there is a way of only displaying the first x characters.. how is this done?

    <clawing through all my mods to try to find how I did it before>
  2. tenants

    tenants Well-Known Member

    Meh.. I cant find it, I'll just have to tidy it up before it hits the template
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    It would be better to round the decimal in the code instead of the template, but xF_Helper_String::wholeWordTrim may help.
  4. Arty

    Arty Well-Known Member

    In view class change that variable to string and remove extra characters:
    class Whatever_ViewPublic_Whatever extends XenForo_ViewPublic_Base
        public function renderHtml()
            if (isset($this->_params['someArray']))
                foreach ($this->_params['someArray'] as &$row)
                    $row['myFloatTrimmed'] = sprintf('%.4F', $row['myFloat']);
    then use {$someArray.myFloatTrimmed}
    tenants likes this.
  5. tenants

    tenants Well-Known Member

    Thanks Arty, I could do this with the view .. I'm sure there was a way only showing the first few charters of a string using one of the css classes or a xen:function (when strings are too long). I cant remember exactly.. but some like, if a search box contains too many words, it's shorted to only show the first few characters and ends in an elsipses


    (but it wasn't search boxes, so it must have been something else)

    It was called all over the place, I didn't want to have to do this every placed it was called
  6. tenants

    tenants Well-Known Member

    note to self (related, but not the question I was asking)

    This will work for strings (not floats):
    {xen:string wordTrim, $myString, 4}
  7. Despair

    Despair Active Member

    @tenants For the float were you looking for xen:number perhaps? {xen:number, 0.059500001370907, 4} should return 0.0595, although it might round the last digit instead of cutting off at 4 decimals I think (i.e. If your number was 0.05955 it might end up being 0.0596)
    tenants likes this.
  8. tenants

    tenants Well-Known Member

    Thanks Despair,

    wordTrim was the one I remember I had used in the past for strings, but xen:number with a 3rd param is probably what I should have been looking for (I ended up formatting the data in the view, so all is good.)

Share This Page