Xplan Quick Xplain
What is floatify and how it works
Let’s look at what this does and why we use it.
Thanks for the questions people are sending in, based on some of those we have put together the following article. Floatify or rather how it’s been used in some templates, has raised questions for people. We were going to cover this off under a larger topic it’s relevant to, but it probably doesn’t hurt to have this as its own item.
Floatify: What it is
Floatify is a built in class that forces the specified data to be returned (converted) into a floating point number.
How it works
Floatify is going to go through the nominated piece of data and filter out anything that is not a digit (0,1,2,3,5,6,7,8,9) and return the result as a float aka, floating point number. The returned result will maintain any decimal places in the original data as well.
An example using some built in data:
An artificial example
<:let mystring=’$550.67 AUD’:>
# You should see: 550.67 as the returned result
The more technical function for how it’s achieved is actually something you’ve likely come across already in standard templates and something that often raises questions from people:
<:let floatify=lambda val: float(filter(lambda x: x.isdigit() or x in ‘.-‘, str(val))):>
Why we use it
Just because something appears as a floating number or currency on screen when in your xplan site, doesn’t mean the data is actually stored that way.
There are many examples through xplan where this is the case: portfolio data, risk researcher and supersolver all store data in ways which will require you to floatify some elements so you can manipulate or output them correctly.
It’s worth noting in brief, that some elements contain methods to get a floating point value eg unformatted and raw data from report sources or .value for currencies – even so you will at times need to use the very handy floatify.
Follow up questions
Some further questions you might have and answers to them.
Isn’t this what float() is for?
Yes float is used and helpful in converting numbers to be floating points but you cannot ‘float’ datatypes that are not native numbers. For example, you can’t float the mystring variable used above, you need floatify to correctly strip out the additional data and convert it to a floating point.
Floatify is built in, so why is <:let floatify…:> defined in the template?
Great observation and question.
In essence it’s not needed anymore. At best it’s depreciated coding that’s just taking up space at worst there’s always the possibility eventually it could interfere with elements of standard functioning.
Why is it still in the templates then?
There was a time when it was needed because floatify was not part of the standard compiler – it wasn’t built in (unlike now).
It keeps appearing in templates because it’s a piece of legacy coding that likely has no impact but as people don’t understand what it does, it just keeps being included rather than removed.
Case in point is the IRESS Risk Researcher template via the template library. Floatify is unnecessarily defined multiple times in the remuneration section. There are other examples of depreciated coding that appear in standard templates, this is one of them and we will get to the others through subsequent articles.