• 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

tenants

Well-known member
#1
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:
<span>{$someArray.myFloat}</span>

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>
 

tyteen4a03

Well-known member
#3
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:
<span>{$someArray.myFloat}</span>

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>
It would be better to round the decimal in the code instead of the template, but xF_Helper_String::wholeWordTrim may help.
 

Arty

Well-known member
#4
In view class change that variable to string and remove extra characters:
Code:
<?php
 
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

Well-known member
#5
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

myrealylongexampleofasillylongstrin...

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

It would be better to round the decimal in the code instead of the template, but xF_Helper_String::wholeWordTrim may help.
It was called all over the place, I didn't want to have to do this every placed it was called
 

tenants

Well-known member
#6
note to self (related, but not the question I was asking)

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

Despair

Active member
#7
@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

Well-known member
#8
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.)